bludit/admin/controllers/edit-user.php

119 lines
2.9 KiB
PHP
Raw Normal View History

2015-05-05 01:00:01 +00:00
<?php defined('BLUDIT') or die('Bludit CMS.');
2015-05-14 22:07:45 +00:00
// ============================================================================
// Functions
// ============================================================================
2015-05-05 01:00:01 +00:00
function editUser($args)
{
global $dbUsers;
2015-07-20 00:14:12 -03:00
global $Language;
2015-08-02 21:49:12 -03:00
2015-07-22 00:15:02 -03:00
if( $dbUsers->set($args) ) {
Alert::set($Language->g('The changes have been saved'));
}
else {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to edit the user.');
}
}
2015-10-20 00:14:28 -03:00
function setPassword($username, $new_password, $confirm_password)
2015-07-22 00:15:02 -03:00
{
global $dbUsers;
global $Language;
2015-10-20 00:14:28 -03:00
if( ($new_password===$confirm_password) && !Text::isEmpty($new_password) )
2015-05-05 01:00:01 +00:00
{
2015-10-20 00:14:28 -03:00
if( $dbUsers->setPassword($username, $new_password) ) {
2015-07-22 00:15:02 -03:00
Alert::set($Language->g('The changes have been saved'));
2015-05-05 01:00:01 +00:00
}
else {
2015-07-22 00:15:02 -03:00
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to change the user password.');
2015-05-05 01:00:01 +00:00
}
}
2015-07-22 00:15:02 -03:00
else {
Alert::set($Language->g('The password and confirmation password do not match'));
return false;
}
}
function deleteUser($args, $deleteContent=false)
{
global $dbUsers;
global $dbPosts;
global $Language;
2015-10-18 19:45:58 -03:00
global $Login;
2015-07-22 00:15:02 -03:00
2015-07-22 22:45:54 -03:00
// The user admin cannot be deleted.
if($args['username']=='admin') {
return false;
}
2015-09-29 22:59:02 -03:00
// The editors cannot delete users.
if($Login->role()!=='admin') {
return false;
}
2015-07-22 00:15:02 -03:00
if($deleteContent) {
$dbPosts->deletePostsByUser($args['username']);
}
else {
$dbPosts->linkPostsToUser($args['username'], 'admin');
}
if( $dbUsers->delete($args['username']) ) {
Alert::set($Language->g('User deleted'));
}
else {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to delete the user.');
2015-05-05 01:00:01 +00:00
}
}
2015-08-02 21:49:12 -03:00
// ============================================================================
// Main before POST
// ============================================================================
2015-05-14 22:07:45 +00:00
// ============================================================================
// POST Method
// ============================================================================
2015-05-05 01:00:01 +00:00
if( $_SERVER['REQUEST_METHOD'] == 'POST' )
{
2015-10-18 19:45:58 -03:00
// Prevent editors to administrate other users.
2015-05-14 22:07:45 +00:00
if($Login->role()!=='admin')
{
$_POST['username'] = $Login->username();
unset($_POST['role']);
}
2015-07-22 00:15:02 -03:00
if(isset($_POST['delete-user-all'])) {
deleteUser($_POST, true);
}
elseif(isset($_POST['delete-user-associate'])) {
deleteUser($_POST, false);
}
2015-10-18 19:45:58 -03:00
elseif( !empty($_POST['new-password']) && !empty($_POST['confirm-password']) ) {
2015-10-20 00:14:28 -03:00
setPassword($_POST['username'], $_POST['new-password'], $_POST['confirm-password']);
2015-07-22 00:15:02 -03:00
}
2015-10-18 19:45:58 -03:00
else {
2015-07-22 00:15:02 -03:00
editUser($_POST);
2015-05-05 01:00:01 +00:00
}
2015-05-14 22:07:45 +00:00
}
// ============================================================================
2015-08-02 21:49:12 -03:00
// Main after POST
2015-05-14 22:07:45 +00:00
// ============================================================================
if($Login->role()!=='admin') {
$layout['parameters'] = $Login->username();
2015-05-05 01:00:01 +00:00
}
2015-07-14 20:57:18 -03:00
$_user = $dbUsers->getDb($layout['parameters']);
2015-05-05 01:00:01 +00:00
// If the user doesn't exist, redirect to the users list.
2015-05-14 22:07:45 +00:00
if($_user===false) {
Redirect::page('admin', 'users');
}
2015-05-30 22:06:55 -03:00
$_user['username'] = $layout['parameters'];