Finished user access switch in users panel.

This commit is contained in:
xevidos 2019-02-10 18:10:21 -05:00
parent 3d122eb296
commit 4298962b14
3 changed files with 312 additions and 283 deletions

View file

@ -410,6 +410,22 @@ class User {
} }
} }
public function update_access() {
global $sql;
$query = "UPDATE users SET access=? WHERE username=?;";
$bind_variables = array( $this->access, $this->username );
$return = $sql->query( $query, $bind_variables, 0, "rowCount" );
if( $return > 0 ) {
echo formatJSEND( "success", "Updated access for {$this->username}" );
} else {
echo formatJSEND( "error", "Error updating project" );
}
}
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
// Verify Account Exists // Verify Account Exists
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////

View file

@ -147,5 +147,18 @@ if ($_GET['action']=='verify') {
if ( $_GET['action'] == 'update_access' ) { if ( $_GET['action'] == 'update_access' ) {
checkSession(); checkSession();
echo json_encode( array( $_GET["username"], $_GET["access"] ) );
if ( ! isset( $_GET['access'] ) || ! isset( $_GET['username'] ) ) {
die( formatJSEND( "error", "Could not update access." ) );
}
if( ! is_admin() ) {
die( formatJSEND( "error", "You do not have permission to update access." ) );
}
$User->username = $_GET["username"];
$User->access = $_GET["access"];
$User->update_access();
} }

View file

@ -1,282 +1,282 @@
/* /*
* Copyright (c) Codiad & Kent Safranski (codiad.com), distributed * Copyright (c) Codiad & Kent Safranski (codiad.com), distributed
* as-is and without warranty under the MIT License. See * as-is and without warranty under the MIT License. See
* [root]/license.txt for more. This information must remain intact. * [root]/license.txt for more. This information must remain intact.
*/ */
(function(global, $) { (function(global, $) {
var codiad = global.codiad; var codiad = global.codiad;
$(function() { $(function() {
codiad.user.init(); codiad.user.init();
}); });
codiad.user = { codiad.user = {
loginForm: $('#login'), loginForm: $('#login'),
controller: 'components/user/controller.php', controller: 'components/user/controller.php',
dialog: 'components/user/dialog.php', dialog: 'components/user/dialog.php',
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
// Initilization // Initilization
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
init: async function() { init: async function() {
var _this = this; var _this = this;
this.loginForm.on('submit', function(e) { this.loginForm.on('submit', function(e) {
e.preventDefault(); e.preventDefault();
_this.authenticate(); _this.authenticate();
}); });
// Get Theme // Get Theme
if(codiad.settings !== undefined) { if(codiad.settings !== undefined) {
var theme = await codiad.settings.get_option('codiad.theme'); var theme = await codiad.settings.get_option('codiad.theme');
} else { } else {
var theme = 'default'; var theme = 'default';
} }
$("#theme option").each(function() { $("#theme option").each(function() {
if($(this).val() == theme) { if($(this).val() == theme) {
$(this).attr("selected", "selected"); $(this).attr("selected", "selected");
} }
}); });
// Get Language // Get Language
if(codiad.settings !== undefined) { if(codiad.settings !== undefined) {
var language = await codiad.settings.get_option('codiad.language'); var language = await codiad.settings.get_option('codiad.language');
} else { } else {
var language = 'en'; var language = 'en';
} }
$("#language option").each(function() { $("#language option").each(function() {
if($(this).val() == language) { if($(this).val() == language) {
$(this).attr("selected", "selected"); $(this).attr("selected", "selected");
} }
}); });
// More Selector // More Selector
$('.show-language-selector').click(function() { $('.show-language-selector').click(function() {
$(this).hide(); $(this).hide();
$('.language-selector').animate({ $('.language-selector').animate({
height: 'toggle' height: 'toggle'
}, "fast"); }, "fast");
}); });
}, },
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
// Authenticate User // Authenticate User
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
authenticate: function() { authenticate: function() {
$.post(this.controller + '?action=authenticate', this.loginForm.serialize(), function(data) { $.post(this.controller + '?action=authenticate', this.loginForm.serialize(), function(data) {
parsed = codiad.jsend.parse(data); parsed = codiad.jsend.parse(data);
if(parsed != 'error') { if(parsed != 'error') {
// Session set, reload // Session set, reload
window.location.reload(); window.location.reload();
} }
}); });
}, },
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
// Logout // Logout
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
logout: function() { logout: function() {
var forcelogout = true; var forcelogout = true;
if($('#list-active-files li.changed').length > 0) { if($('#list-active-files li.changed').length > 0) {
forcelogout = confirm(i18n('You have unsaved files.')); forcelogout = confirm(i18n('You have unsaved files.'));
} }
if(forcelogout) { if(forcelogout) {
$('#list-active-files li.changed').each(function() { $('#list-active-files li.changed').each(function() {
$(this).removeClass('changed') $(this).removeClass('changed')
}); });
amplify.publish('user.logout', {}); amplify.publish('user.logout', {});
codiad.settings.save(); codiad.settings.save();
$.get(this.controller + '?action=logout', function() { $.get(this.controller + '?action=logout', function() {
window.location.reload(); window.location.reload();
}); });
} }
}, },
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
// Open the user manager dialog // Open the user manager dialog
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
list: function() { list: function() {
$('#modal-content form') $('#modal-content form')
.die('submit'); // Prevent form bubbling .die('submit'); // Prevent form bubbling
codiad.modal.load(600, this.dialog + '?action=list'); codiad.modal.load(600, this.dialog + '?action=list');
}, },
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
// Create User // Create User
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
createNew: function() { createNew: function() {
var _this = this; var _this = this;
codiad.modal.load(400, this.dialog + '?action=create'); codiad.modal.load(400, this.dialog + '?action=create');
$('#modal-content form') $('#modal-content form')
.live('submit', function(e) { .live('submit', function(e) {
e.preventDefault(); e.preventDefault();
var pass = true; var pass = true;
var username = $('#modal-content form input[name="username"]') var username = $('#modal-content form input[name="username"]')
.val(); .val();
var password1 = $('#modal-content form input[name="password1"]') var password1 = $('#modal-content form input[name="password1"]')
.val(); .val();
var password2 = $('#modal-content form input[name="password2"]') var password2 = $('#modal-content form input[name="password2"]')
.val(); .val();
// Check matching passwords // Check matching passwords
if(password1 != password2) { if(password1 != password2) {
codiad.message.error(i18n('Passwords Do Not Match')); codiad.message.error(i18n('Passwords Do Not Match'));
pass = false; pass = false;
} }
// Check no spaces in username // Check no spaces in username
if(!/^[a-z0-9]+$/i.test(username) || username.length === 0) { if(!/^[a-z0-9]+$/i.test(username) || username.length === 0) {
codiad.message.error(i18n('Username Must Be Alphanumeric String')); codiad.message.error(i18n('Username Must Be Alphanumeric String'));
pass = false; pass = false;
} }
if(pass) { if(pass) {
$.post(_this.controller + '?action=create', { $.post(_this.controller + '?action=create', {
'username': username, 'username': username,
'password': password1 'password': password1
}, function(data) { }, function(data) {
var createResponse = codiad.jsend.parse(data); var createResponse = codiad.jsend.parse(data);
if(createResponse != 'error') { if(createResponse != 'error') {
codiad.message.success(i18n('User Account Created')) codiad.message.success(i18n('User Account Created'))
_this.list(); _this.list();
} }
}); });
} }
}); });
}, },
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
// Delete User // Delete User
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
delete: function(username) { delete: function(username) {
var _this = this; var _this = this;
codiad.modal.load(400, this.dialog + '?action=delete&username=' + username); codiad.modal.load(400, this.dialog + '?action=delete&username=' + username);
$('#modal-content form') $('#modal-content form')
.live('submit', function(e) { .live('submit', function(e) {
e.preventDefault(); e.preventDefault();
var username = $('#modal-content form input[name="username"]') var username = $('#modal-content form input[name="username"]')
.val(); .val();
codiad.modal.show_loading(); codiad.modal.show_loading();
$.get(_this.controller + '?action=delete&username=' + username, function(data) { $.get(_this.controller + '?action=delete&username=' + username, function(data) {
var deleteResponse = codiad.jsend.parse(data); var deleteResponse = codiad.jsend.parse(data);
if(deleteResponse != 'error') { if(deleteResponse != 'error') {
codiad.message.success(i18n('Account Deleted')) codiad.message.success(i18n('Account Deleted'))
_this.list(); _this.list();
} }
}); });
}); });
}, },
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
// Set Project Access // Set Project Access
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
projects: function(username) { projects: function(username) {
codiad.modal.load(400, this.dialog + '?action=projects&username=' + username); codiad.modal.load(400, this.dialog + '?action=projects&username=' + username);
var _this = this; var _this = this;
$('#modal-content form') $('#modal-content form')
.live('submit', function(e) { .live('submit', function(e) {
e.preventDefault(); e.preventDefault();
var username = $('#modal-content form input[name="username"]') var username = $('#modal-content form input[name="username"]')
.val(); .val();
var accessLevel = $('#modal-content form select[name="access_level"]') var accessLevel = $('#modal-content form select[name="access_level"]')
.val(); .val();
var projects = new Array(); var projects = new Array();
$('input:checkbox[name="project"]:checked').each(function() { $('input:checkbox[name="project"]:checked').each(function() {
projects.push($(this).val()); projects.push($(this).val());
}); });
if(accessLevel == 0) { if(accessLevel == 0) {
projects = 0; projects = 0;
} }
// Check and make sure if access level not full that at least on project is selected // Check and make sure if access level not full that at least on project is selected
if(accessLevel == 1 && !projects) { if(accessLevel == 1 && !projects) {
codiad.message.error(i18n('At Least One Project Must Be Selected')); codiad.message.error(i18n('At Least One Project Must Be Selected'));
} else { } else {
$.post(_this.controller + '?action=project_access&username=' + username, { $.post(_this.controller + '?action=project_access&username=' + username, {
projects: projects projects: projects
}, function(data) { }, function(data) {
var projectsResponse = codiad.jsend.parse(data); var projectsResponse = codiad.jsend.parse(data);
if(projectsResponse != 'error') { if(projectsResponse != 'error') {
codiad.message.success(i18n('Account Modified')); codiad.message.success(i18n('Account Modified'));
} }
}); });
} }
}); });
}, },
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
// Change Password // Change Password
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
password: function(username) { password: function(username) {
var _this = this; var _this = this;
codiad.modal.load(400, this.dialog + '?action=password&username=' + username); codiad.modal.load(400, this.dialog + '?action=password&username=' + username);
$('#modal-content form') $('#modal-content form')
.live('submit', function(e) { .live('submit', function(e) {
e.preventDefault(); e.preventDefault();
var username = $('#modal-content form input[name="username"]') var username = $('#modal-content form input[name="username"]')
.val(); .val();
var password1 = $('#modal-content form input[name="password1"]') var password1 = $('#modal-content form input[name="password1"]')
.val(); .val();
var password2 = $('#modal-content form input[name="password2"]') var password2 = $('#modal-content form input[name="password2"]')
.val(); .val();
if(password1 != password2) { if(password1 != password2) {
codiad.message.error(i18n('Passwords Do Not Match')); codiad.message.error(i18n('Passwords Do Not Match'));
} else { } else {
$.post(_this.controller + '?action=password', { $.post(_this.controller + '?action=password', {
'username': username, 'username': username,
'password': password1 'password': password1
}, function(data) { }, function(data) {
var passwordResponse = codiad.jsend.parse(data); var passwordResponse = codiad.jsend.parse(data);
if(passwordResponse != 'error') { if(passwordResponse != 'error') {
codiad.message.success(i18n('Password Changed')); codiad.message.success(i18n('Password Changed'));
codiad.modal.unload(); codiad.modal.unload();
} }
}); });
} }
}); });
}, },
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
// Change Current Project // Change Current Project
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
project: function(project) { project: function(project) {
$.get(this.controller + '?action=project&project=' + project); $.get(this.controller + '?action=project&project=' + project);
}, },
update_access: function( e, username=null ) { update_access: function( e, username=null ) {
let access = ""; let access = "";
if( ( typeof e ) == "string" ) { if( ( typeof e ) == "string" ) {
access = e; access = e;
} else { } else {
access = e.target.value; access = e.target.value;
} }
$.get( this.controller + `?action=update_access&username=${username}&access=${access}`, function( data ) { $.get( this.controller + `?action=update_access&username=${username}&access=${access}`, function( data ) {
let response = codiad.jsend.parse( data ); let response = codiad.jsend.parse( data );
if( response != 'error' ) { if( response != 'error' ) {
codiad.message.success( i18n( 'Access Updated' ) ); codiad.message.success( i18n( 'Access Updated' ) );
} }
}); });
}, },
}; };
})(this, jQuery); })(this, jQuery);