Skip to content

Commit

Permalink
Merge branch 'hotfix/permissions'
Browse files Browse the repository at this point in the history
  • Loading branch information
polonel committed Mar 17, 2019
2 parents bacc5e3 + 62c87c9 commit c6de5d0
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 17 deletions.
2 changes: 1 addition & 1 deletion public/js/9.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/client/containers/Accounts/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class AccountsContainer extends React.Component {
<span className='tru-list-heading'>Groups</span>
<span className='uk-text-small uk-text-muted uk-text-truncate'>
{user.get('groups').map(group => {
return group.get('name') + (user.get('groups').length > 1 ? ', ' : '')
return group.get('name') + (user.get('groups').toArray().length > 1 ? ', ' : '')
})}
</span>
</div>
Expand Down
43 changes: 40 additions & 3 deletions src/client/containers/Settings/Permissions/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ import PropTypes from 'prop-types'
import { connect } from 'react-redux'

import { showModal, fetchRoles, updateRoleOrder } from 'actions/common'
import { updateSetting } from 'actions/settings'

import Button from 'components/Button'
import SettingItem from 'components/Settings/SettingItem'
import SingleSelect from 'components/SingleSelect'
import SplitSettingsPanel from 'components/Settings/SplitSettingsPanel'
import PermissionBody from './permissionBody'

Expand All @@ -29,6 +32,12 @@ class PermissionsSettingsContainer extends React.Component {
this.props.fetchRoles()
}

getSetting (name) {
return this.props.settings.getIn(['settings', name, 'value'])
? this.props.settings.getIn(['settings', name, 'value'])
: ''
}

onRoleOrderChanged (e) {
const children = $(e.target).children('li')
let arr = []
Expand Down Expand Up @@ -57,9 +66,34 @@ class PermissionsSettingsContainer extends React.Component {
this.props.showModal('CREATE_ROLE')
}

onDefaultUserRoleChange (e) {
this.props.updateSetting({ name: 'role:user:default', value: e.target.value, stateName: 'defaultUserRole' })
}

render () {
const mappedRoles = this.props.roles
.map(role => {
return { text: role.get('name'), value: role.get('_id') }
})
.toArray()

return (
<div className={this.props.active ? '' : 'hide'}>
<SettingItem
title={'Default New User Role'}
subtitle={'Role assigned to users created during sign-up and public tickets'}
component={
<SingleSelect
items={mappedRoles}
defaultValue={this.getSetting('defaultUserRole')}
onSelectChange={e => {
this.onDefaultUserRoleChange(e)
}}
width={'50%'}
showTextbox={false}
/>
}
/>
<SplitSettingsPanel
title={'Permissions'}
tooltip={'Permission order is top down. ex: Admins at top; Users at bottom.'}
Expand Down Expand Up @@ -95,17 +129,20 @@ PermissionsSettingsContainer.propTypes = {
active: PropTypes.bool.isRequired,
roles: PropTypes.object.isRequired,
roleOrder: PropTypes.object.isRequired,
settings: PropTypes.object.isRequired,
fetchRoles: PropTypes.func.isRequired,
updateRoleOrder: PropTypes.func.isRequired,
showModal: PropTypes.func.isRequired
showModal: PropTypes.func.isRequired,
updateSetting: PropTypes.func.isRequired
}

const mapStateToProps = state => ({
roles: state.shared.roles,
roleOrder: state.shared.roleOrder
roleOrder: state.shared.roleOrder,
settings: state.settings.settings
})

export default connect(
mapStateToProps,
{ fetchRoles, updateRoleOrder, showModal }
{ fetchRoles, updateRoleOrder, showModal, updateSetting }
)(PermissionsSettingsContainer)
15 changes: 14 additions & 1 deletion src/controllers/api/v1/tickets.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,18 @@ apiTickets.createPublicTicket = function (req, res) {
async.waterfall(
[
function (next) {
var settingSchmea = require('../../../models/setting')
settingSchmea.getSetting('role:user:default', function (err, roleDefault) {
if (err) return next(err)
if (!roleDefault) {
winston.error('No Default User Role Set. (Settings > Permissions > Default User Role)')
return next('No Default Role Set')
}

return next(null, roleDefault)
})
},
function (roleDefault, next) {
var UserSchema = require('../../../models/user')
plainTextPass = chance.string({
length: 6,
Expand All @@ -444,7 +456,8 @@ apiTickets.createPublicTicket = function (req, res) {
password: plainTextPass,
fullname: postData.user.fullname,
email: postData.user.email,
role: 'user'
accessToken: chance.hash(),
role: roleDefault.value
})

user.save(function (err, savedUser) {
Expand Down
18 changes: 17 additions & 1 deletion src/controllers/api/v1/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,15 @@ apiUsers.create = function (req, res) {
if (postData.aPass !== postData.aPassConfirm)
return res.status(400).json({ success: false, error: 'Invalid Password Match' })

var Chance = require('chance')
var chance = new Chance()

var account = new UserSchema({
username: postData.aUsername,
password: postData.aPass,
fullname: postData.aFullname,
email: postData.aEmail,
accessToken: chance.hash(),
role: postData.aRole
})

Expand Down Expand Up @@ -268,13 +272,25 @@ apiUsers.createPublicAccount = function (req, res) {
async.waterfall(
[
function (next) {
var SettingSchema = require('../../../models/setting')
SettingSchema.getSetting('role:user:default', function (err, roleDefault) {
if (err) return next(err)
if (!roleDefault) {
winston.error('No Default User Role Set. (Settings > Permissions > Default User Role)')
return next({ message: 'No Default Role Set. Please contact administrator.' })
}

return next(null, roleDefault)
})
},
function (roleDefault, next) {
var UserSchema = require('../../../models/user')
user = new UserSchema({
username: postData.user.email,
password: postData.user.password,
fullname: postData.user.fullname,
email: postData.user.email,
role: 'user'
role: roleDefault.value
})

user.save(function (err, savedUser) {
Expand Down
4 changes: 4 additions & 0 deletions src/sass/partials/tickets.sass
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ div#accountsTable_wrapper

.input-box
margin-bottom: 15px
select > option
background: $page_content_right_bg
color: automatic-text-color($page_content_right_bg)
border: 1px solid $page_content_border_color

.history-items
//height: 185px
Expand Down
43 changes: 40 additions & 3 deletions src/settings/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,21 @@ function rolesDefault (callback) {
description: 'Default role for users',
grants: settingsDefaults.userGrants
},
done
function (err, userRole) {
if (err) return done(err)
SettingsSchema.getSetting('role:user:default', function (err, roleUserDefault) {
if (err) return done(err)
if (roleUserDefault) return done()

SettingsSchema.create(
{
name: 'role:user:default',
value: userRole._id
},
done
)
})
}
)
})
},
Expand Down Expand Up @@ -166,6 +180,28 @@ function rolesDefault (callback) {
)
}

function defaultUserRole (callback) {
var roleOrderSchema = require('../models/roleorder')
roleOrderSchema.getOrder(function (err, roleOrder) {
if (err) return callback(err)
if (!roleOrder) return callback()

SettingsSchema.getSetting('role:user:default', function (err, roleDefault) {
if (err) return callback(err)
if (roleDefault) return callback()

var lastId = _.last(roleOrder.order)
SettingsSchema.create(
{
name: 'role:user:default',
value: lastId
},
callback
)
})
})
}

function createDirectories (callback) {
async.parallel(
[
Expand Down Expand Up @@ -393,9 +429,7 @@ function normalizeTags (callback) {
function checkPriorities (callback) {
var ticketSchema = require('../models/ticket')
var migrateP1 = false

var migrateP2 = false

var migrateP3 = false

async.parallel(
Expand Down Expand Up @@ -594,6 +628,9 @@ settingsDefaults.init = function (callback) {
function (done) {
return rolesDefault(done)
},
function (done) {
return defaultUserRole(done)
},
function (done) {
return timezoneDefault(done)
},
Expand Down
7 changes: 4 additions & 3 deletions src/settings/json/mailer-new-ticket.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/settings/settingsUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ util.getSettings = function (callback) {
s.minSubjectLength = parseSetting(settings, 'ticket:minlength:subject', 10)
s.minIssueLength = parseSetting(settings, 'ticket:minlength:issue', 10)

s.defaultUserRole = parseSetting(settings, 'role:user:default', '')

s.mailerEnabled = parseSetting(settings, 'mailer:enable', false)
s.mailerHost = parseSetting(settings, 'mailer:host', '')
s.mailerSSL = parseSetting(settings, 'mailer:ssl', false)
Expand Down
26 changes: 22 additions & 4 deletions src/views/pub_signup.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,17 @@
</a>
</div>
</div>
<div id="accountCreateError" class="hide">
<h2 style="margin-bottom: 25px; text-align: left;">An Error has occurred</h2>
<p style="text-align: justify;">
An Unknown Error has occurred
</p>
<div style="width: 100%; margin-top: 50px;">
<a href="/signup" class="no-ajaxy" style="width: 100%; padding: 0; margin: 0;">
<button class="btn md-btn md-btn-wave md-btn-danger" type="button" style="width: 100%; margin-bottom: 10px !important;">Start Over</button>
</a>
</div>
</div>
</div>
</div>

Expand Down Expand Up @@ -522,7 +533,7 @@
error: function(e) {
$checkEmailBtn.prop('disabled', false).text('Next');
window.Snackbar.show({
text: 'An Error occurred.',
text: 'Invalid Captcha',
actionTextColor: 'red'
});
Expand Down Expand Up @@ -647,19 +658,26 @@
url: '/api/v1/public/account/create',
data: JSON.stringify(data),
contentType: 'application/json',
error: function(response) {
console.log(response)
$('.activeSlide').removeClass('activeSlide').addClass('hide')
var $errorSlide = $('#accountCreateError')
$errorSlide.removeClass('hide')
$errorSlide.find('p').html(response.responseJSON.error)
},
success: function(response) {
if (!response.success) {
//TODO: Show Error Slide
var $errorSlide = $('#accountCreateError')
$errorSlide.removeClass('hide')
$errorSlide.find('p').html('Error: ' + response.error)
} else {
$('span#spanfullname').text(userInfo.fullname);
$('span#spanemail').text(userInfo.email);
showAccountCreated();
}
},
error: function(err) {
console.error(err);
}
})
}
Expand Down

0 comments on commit c6de5d0

Please sign in to comment.