actiondeletion.php 6.1 KB
Newer Older
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
<?php
/**
 *
 * @package    mahara
 * @subpackage core
 * @author     Stacey Walker
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
 *
 */

define('INTERNAL', 1);
define('INSTITUTIONALADMIN', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'admin');
define('SECTION_PAGE', 'actiondeletion');
require_once('institution.php');

$id = param_integer('d');
$action = param_alpha('action');

if (!is_logged_in()) {
    throw new AccessDeniedException();
}

if (!$deletion = get_record_select('usr_pendingdeletion', '"id" = ?', array($id))) {
    die_info(get_string('userdeletionnosuchid', 'auth.internal'));
}

$usertodelete = new User();
$usertodelete->find_by_id($deletion->usr);

if ($action == 'approve') {
    $message = get_string('approveuserdeletionmessage', 'admin', $usertodelete->username);
    $submitbtn = get_string('approve', 'admin');
    define('TITLE', get_string('approveuserdeletionfor', 'admin',
        $usertodelete->firstname, $usertodelete->lastname, $usertodelete->email));
}
else {
    $message = get_string('denyuserdeletionmessage', 'admin');
    $submitbtn = get_string('deny', 'admin');
    define('TITLE', get_string('denyuserdeletionfor', 'admin',
        $usertodelete->firstname, $usertodelete->lastname));
    $elements['message'] = array(
        'type'  => 'textarea',
        'title' => get_string('deletiondeniedreason', 'admin'),
        'description' => get_string('deletiondeniedreasondesc', 'admin'),
        'cols' => 50,
        'rows' => 10,
    );
}
foreach ((array)$deletion as $key => $value) {
    $elements[$key] = array(
        'type'  => 'hidden',
        'value' => $value,
    );
}
$elements['submit'] = array(
    'type' => 'submitcancel',
    'value' => array($submitbtn, get_string('cancel')),
    'class' => 'btn-primary',
    'goto' => get_config('wwwroot') . 'admin/users/pendingdeletions.php'
);

$form = pieform(array(
    'name' => $action.'deletion',
    'autofocus' => false,
    'method' => 'post',
    'elements' => $elements,
));

$smarty = smarty();
$smarty->assign('message', $message);
$smarty->assign('form', $form);
$smarty->display('admin/users/actiondeletion.tpl');

function denydeletion_submit(Pieform $form, $values) {
    global $USER, $SESSION, $deletion, $usertodelete;

    if (isset($values['message']) && !empty($values['message'])) {
        $message = get_string('userdeletiondeniedmessagereason', 'auth.internal',
            $usertodelete->firstname, get_config('sitename'), $values['message'], display_name($USER));
    }
    else {
        $message = get_string('userdeletiondeniedmessage', 'auth.internal',
            $usertodelete->firstname, get_config('sitename'), display_name($USER));
    }
    try {
        delete_records('usr_pendingdeletion', 'id', $values['id']);

        email_user($usertodelete, $USER,
            get_string('userdeletiondeniedemailsubject', 'auth.internal', get_config('sitename')),
            $message
        );
    }
    catch (EmailException $e) {
        log_warn($e);
        die_info(get_string('userdeletiondeniedunsuccessful', 'admin'));
    }
    catch (SQLException $e) {
        log_warn($e);
        die_info(get_string('userdeletiondeniedunsuccessful', 'admin'));
    }

    $SESSION->add_ok_msg(get_string('userdeletiondeniedsuccessful', 'admin'));
    redirect('/admin/users/pendingdeletions.php');
}

function approvedeletion_submit(Pieform $form, $values) {
    global $SESSION, $usertodelete, $USER;

    // cant delete the last site admin
    $admins = get_site_admins();
    $lastadminid = 0;
    if (count($admins)== 1) {
        $lastadminid = $admins[0]->id;
    }

    $usercanbedeleted = $candeleteuser = false;
    // Check if user can be deleted
    if (isset($values['id']) && isset($values['usr'])
        && ($values['usr'] != 0)
        && ($values['usr'] != $USER->get('id'))
        && ($values['usr'] != $lastadminid)
        && ($usrdeletion = get_record('usr_pendingdeletion', 'id', $values['id']))
        && ($usrdeletion->usr == $values['usr'])) {
        $usercanbedeleted = true;
    }

    if ($usercanbedeleted) {
        // Now check if we are allowed to delete them
        $userinstitutions = $usertodelete->get('institutions');
        if (empty($userinstitutions) && $USER->get('admin')) {
            // we are only in 'mahara' institution so can only be deleted by site admins
            $candeleteuser = true;
        }
        else {
            foreach ($userinstitutions as $i) {
                if ($USER->can_edit_institution($i->institution)) {
                    // If $USER can edit any of the institutions that the $user belongs then they are allowed to delete the user
                    $candeleteuser = true;
                    break;
                }
            }
        }
    }

    if ($usercanbedeleted && $candeleteuser) {
        delete_records('usr_pendingdeletion', 'id', $values['id']);

        //delete user account
        delete_user($values['usr']);

        // send the user the official account deletion email
        email_user(
                $usertodelete,
                null,
                get_string('userdeletionemailsubject', 'auth.internal', get_config('sitename')),
                get_string(
                        'userdeletionemailmessagetext',
                        'auth.internal',
                        $usertodelete->firstname,
                        get_config('sitename'),
                        get_config('sitename')
                ),
                get_string(
                        'userdeletionemailmessagehtml',
                        'auth.internal',
                        $usertodelete->firstname,
                        get_config('sitename'),
                        get_config('sitename')
                )
        );

        $SESSION->add_ok_msg(get_string('deletionapprovedsuccessfully', 'admin'));
    }
    else {
        $SESSION->add_error_msg(get_string('deletionapprovedfailed', 'admin'));
    }
    redirect('/admin/users/pendingdeletions.php');
}