File size: 10,985 Bytes
eb67da4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
/*
* . .o8 oooo
* .o8 "888 `888
* .o888oo oooo d8b oooo oooo .oooo888 .ooooo. .oooo.o 888 oooo
* 888 `888""8P `888 `888 d88' `888 d88' `88b d88( "8 888 .8P'
* 888 888 888 888 888 888 888ooo888 `"Y88b. 888888.
* 888 . 888 888 888 888 888 888 .o o. )88b 888 `88b.
* "888" d888b `V88V"V8P' `Y8bod88P" `Y8bod8P' 8""888P' o888o o888o
* ========================================================================
* Author: Chris Brame
* Updated: 1/20/19 4:43 PM
* Copyright (c) 2014-2019. All rights reserved.
*/
define([
'angular',
'underscore',
'jquery',
'modules/helpers',
'uikit',
'qrcode',
'history',
'angularjs/services/session'
], function (angular, _, $, helpers, UIKit) {
return angular
.module('trudesk.controllers.profile', ['trudesk.services.session'])
.controller('profileCtrl', function (SessionService, $scope, $window, $document, $http, $log, $timeout) {
var otpEnabled = false
$scope.init = function () {
// Fix Inputs if input is preloaded with a value
fixInputLabels()
otpEnabled = $scope.otpEnabled
}
function fixInputLabels () {
$timeout(function () {
$('input.md-input').each(function () {
var vm = this
var self = $(vm)
if (!_.isEmpty(self.val())) {
var s = self.parent('.md-input-wrapper')
if (s.length > 0) {
s.addClass('md-input-filled')
}
}
})
}, 0)
}
function validateEmail (email) {
return String(email)
.toLowerCase()
.match(
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
)
}
$scope.updateUser = function ($event) {
$event.preventDefault()
var id = $('div[data-user_id]').attr('data-user_id')
if (_.isUndefined(id)) return
var data = getFormData()
if (
data.fullname.toString().length > 25 ||
data.password.toString().length > 255 ||
data.cPassword.toString().length > 255 ||
data.email.toString().length > 255 ||
!validateEmail(data.email.toString())
) {
helpers.UI.showSnackbar('Form data invalid.', true)
return false
}
$http
// BUG: CWE-269 Improper Privilege Management
// .put('/api/v1/users/' + data.username, {
// FIXED:
.put('/api/v1/profile/', {
aId: id,
aFullname: data.fullname,
aPass: data.password,
aPassConfirm: data.cPassword,
aEmail: data.email,
saveGroups: false
})
.success(function () {
resetForm()
helpers.UI.showSnackbar({
text: 'Profile Successfully Saved',
textColor: '#f8f8f2'
})
})
.error(function (e) {
if (e.error.message) {
$log.log('[trudesk:profile:updateUser] - ' + e.error.message)
helpers.UI.showSnackbar('Error ' + e.error.message, true)
} else {
$log.log('[trudesk:profile:updateUser] - ' + e.error)
helpers.UI.showSnackbar('Error: ' + e.error, true)
}
})
}
$scope.showTour = function () {
var username = SessionService.getUser().username
$http
.put('/api/v1/users/' + username + '/updatepreferences', {
preference: 'tourCompleted',
value: false
})
.success(function () {
$window.location.href = '/'
})
.error(function (e) {
$log.log('[trudesk:profile:showTour] - ' + e.error.message)
helpers.UI.showSnackbar('Error ' + e.error.message, true)
})
}
$scope.back = function ($event) {
History.go(-1)
$event.preventDefault()
}
$scope.generateApiKey = function ($event) {
$event.preventDefault()
var id = $('div[data-user_id]').attr('data-user_id')
if (_.isUndefined(id)) return
$http
.post('/api/v1/users/' + id + '/generateapikey')
.success(function (tokenJson) {
$('#aApiKey').val(tokenJson.token)
$('.removeApiButton').removeClass('hide')
$('.generateApiButton').addClass('hide')
// helpers.showFlash('API Key Successfully Generated');
helpers.UI.showSnackbar('API Key Successfully Generated', false)
})
.error(function (e) {
$log.log('[trudesk:profile:generateApiKey] - ' + e)
// helpers.showFlash('Error: ' + e, true);
helpers.UI.showSnackbar('Error: Unable to generate API Key!', true)
})
}
$scope.removeApiKey = function ($event) {
$event.preventDefault()
var id = $('div[data-user_id]').attr('data-user_id')
if (_.isUndefined(id)) return
$http
.post('/api/v1/users/' + id + '/removeapikey')
.success(function () {
$('#aApiKey').val('')
$('.generateApiButton').removeClass('hide')
$('.removeApiButton').addClass('hide')
helpers.UI.showSnackbar('API Key Successfully Revoked', false)
})
.error(function (e) {
$log.log('[trudesk:profile:removeApiKey]', e)
helpers.UI.showSnackbar('Error: Unable to remove API Key!', true)
})
}
$scope.otpChange = function (event) {
var $totpSettings = $('.totp-settings-wrap')
var $totpPanel = $totpSettings.find('.panel-body2')
var $tOTPKey = $totpSettings.find('#tOTPKey')
var $qrCode = $totpSettings.find('#totp-qrcode')
event.preventDefault()
if (otpEnabled) {
UIKit.modal.confirm(
'<span style="font-size: 16px; color: #FF9800;">WARNING: Disabling Two Factor Authentication will remove your shared secret. A new key will generate when re-enabled.</span><br />' +
'Are you sure you want to disable two factor authentication?',
function () {
removeL2Auth(function (err) {
if (err) {
$log.error(err)
}
angular.element(event.target).attr('checked', false)
$totpPanel.slideUp(400, function () {
$totpPanel.css({ overflow: 'hidden', margin: 0 })
$qrCode.find('canvas').remove()
$tOTPKey.val()
$timeout(function () {
otpEnabled = false
}, 0)
})
})
},
{
labels: { Ok: 'Yes', Cancel: 'No' }
}
)
} else {
generateL2Auth(function (err, key) {
if (err) {
$log.error(err)
helpers.UI.showSnackbar('An unknown error occurred. Check console.', true)
return
}
$timeout(function () {
otpEnabled = true
angular.element(event.target).prop('checked', true)
}, 0)
var host = $('div[data-host]').attr('data-host')
var username = SessionService.getUser().username
var qrKey =
'otpauth://totp/' +
host +
'-' +
username +
':' +
host +
'-' +
username +
'?secret=' +
key +
'&issuer=Trudesk'
$qrCode.qrcode({ width: 242, height: 242, text: qrKey })
$tOTPKey.val(key)
$totpPanel.css({ margin: '10px 7px 7px 7px' })
$totpPanel.find('input').removeClass('hide')
$totpPanel.removeClass('hide')
fixInputLabels()
$totpPanel.slideDown()
// }
})
}
}
function generateL2Auth (completed) {
var id = SessionService.getUser()._id
if (_.isUndefined(id)) {
return helpers.UI.showSnackbar('Unable to get user ID.', true)
}
$http
.post(
'/api/v1/users/' + id + '/generatel2auth',
{},
{
headers: {
'CSRF-TOKEN': $document[0].querySelector('meta[name="csrf-token"]').getAttribute('content')
}
}
)
.then(
function success (response) {
if (!response.data.success) {
helpers.UI.showSnackbar('Error: Unknown error has occurred.', true)
if (_.isFunction(completed)) {
return completed('Error: Unknown error has occurred.')
}
} else {
// Success
if (_.isFunction(completed)) {
completed(null, response.data.generatedKey)
}
}
},
function error (err) {
$log.error('[trudesk:profile:generateL2Auth]')
$log.error(err)
helpers.UI.showSnackbar('Error: Could not generate new secret! Check Console', true)
if (_.isFunction(completed)) {
completed(err)
}
}
)
}
function removeL2Auth (completed) {
var id = SessionService.getUser()._id
if (_.isUndefined(id)) {
return helpers.UI.showSnackbar('Unable to get user ID.', true)
}
$http
.post(
'/api/v1/users/' + id + '/removel2auth',
{},
{
headers: {
'CSRF-TOKEN': $document[0].querySelector('meta[name="csrf-token"]').getAttribute('content')
}
}
)
.success(function () {
if (_.isFunction(completed)) {
completed()
}
})
.error(function (e) {
$log.error('[trudesk:profile:removeL2Auth]')
$log.error(e)
helpers.UI.showSnackbar('Error: Could not remove. Check Console', true)
if (_.isFunction(completed)) {
completed(e)
}
})
}
function getFormData () {
var data = {}
data.username = $('#aUsername').val()
data.fullname = $('#aFullname').val()
data.password = $('#aPass').val()
data.cPassword = $('#aPassConfirm').val()
data.email = $('#aEmail').val()
return data
}
function resetForm () {
$('#aPass').val('')
$('#aPassConfirm').val('')
}
})
})
|