Issues (1965)

html/user/manage_project.php (3 issues)

1
<?php
2
3
// This file is part of BOINC.
4
// http://boinc.berkeley.edu
5
// Copyright (C) 2011 University of California
6
//
7
// BOINC is free software; you can redistribute it and/or modify it
8
// under the terms of the GNU Lesser General Public License
9
// as published by the Free Software Foundation,
10
// either version 3 of the License, or (at your option) any later version.
11
//
12
// BOINC is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
// See the GNU Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public License
18
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
19
20
// Interface for project-wide functions:
21
//   - control user quotas and permissions to submit jobs
22
//   - create apps (not implemented yet)
23
24
require_once("../inc/submit_db.inc");
25
require_once("../inc/util.inc");
26
require_once("../inc/keywords.inc");
27
require_once("../inc/kw_prefs.inc");
28
29
function user_row($u) {
30
    $user = BoincUser::lookup_id($u->user_id);
31
    $uas = BoincUserSubmitApp::enum("user_id=$u->user_id");
32
33
    if ($u->submit_all) {
34
        $sub = 'All applications';
35
    } else {
36
        $names = [];
37
        foreach ($uas as $ua) {
38
            $app = BoincApp::lookup_id($ua->app_id);
39
            $names[] = $app->name;
40
        }
41
        $sub = $names?implode(', ', $names):'---';
42
    }
43
    if ($u->manage_all) {
44
        $admin = 'All applications';
45
    } else {
46
        $names = [];
47
        foreach ($uas as $ua) {
48
            if (!$ua->manage) continue;
49
            $app = BoincApp::lookup_id($ua->app_id);
50
            $names[] = $app->name;
51
        }
52
        $admin = $names?implode(', ', $names):'---';
53
    }
54
    [$yes, $no] = read_kw_prefs($user);
55
    global $job_keywords;
56
    $kws = [];
57
    foreach ($yes as $id) {
58
        $kw = $job_keywords[$id];
59
        $kws[] = $kw->name;
60
    }
61
62
    table_row(
63
        sprintf(
64
            '<a href=manage_project.php?action=edit_form&user_id=%d>%s</a>',
65
            $u->user_id, $user->name
66
        ),
67
        $sub,
68
        $admin,
69
        implode('<br>', $kws),
70
        $u->quota,
71
        $u->max_jobs_in_progress,
72
        ($u->logical_start_time > time())?local_time_str($u->logical_start_time):'---'
73
    );
74
}
75
76
function handle_list() {
77
    page_head("Job submission access control");
78
    echo "The following users are allowed to submit jobs.
79
        <p>
80
    ";
81
82
    $us = BoincUserSubmit::enum("");
83
    start_table('table-striped');
84
    table_header(
85
        "User<br><small>Click to change permissions or quota</small>",
86
        "Can submit jobs to",
87
        "Can administer apps for",
88
        'Keywords',
89
        "Quota",
90
        "Max jobs in progress<br><small>0 means no limit</small>",
91
        "Current priority<br><small>Later time = lower priority</small>"
92
    );
93
    foreach ($us as $u) {
94
        user_row($u);
95
    }
96
    end_table();
97
    show_button("manage_project.php?action=add_form",
98
        "Add user", "Allow a new user to submit jobs"
99
    );
100
    page_tail();
101
}
102
103
// get multi-select lists for apps
104
//
105
function get_app_lists($user_id) {
106
    $items = [];
107
    $submit = [];
108
    $manage = [];
109
    $apps = BoincApp::enum("deprecated=0");
110
    foreach ($apps as $app) {
111
        $items[] = [$app->id, $app->name];
112
        $us = BoincUserSubmitApp::lookup("user_id=$user_id and app_id=$app->id");
113
        if ($us) {
114
            $submit[] = $app->id;
115
            if ($us->manage) {
116
                $manage[] = $app->id;
117
            }
118
        }
119
    }
120
    return [$items, $submit, $manage];
121
}
122
123
function handle_edit_form() {
124
    $user_id = get_int('user_id');
125
    $user = BoincUser::lookup_id($user_id);
126
    $usub = BoincUserSubmit::lookup_userid($user_id);
127
    page_head_select2("Job submission permissions for $user->name");
128
    form_start('manage_project.php');
129
    form_input_hidden('action', 'edit_action');
130
    form_input_hidden('user_id', $user->id);
131
    [$apps, $submit, $manage] = get_app_lists($user_id);
132
    form_radio_buttons('Can submit jobs to', 'submit_all',
133
        [[1, 'All apps'], [0, 'Only selected apps']],
134
        $usub->submit_all, true
135
    );
136
    form_select2_multi(
137
        '', 'submit_apps', $apps, $submit, "id=submit_apps"
138
    );
139
    form_radio_buttons('Can administer', 'manage_all',
140
        [[1, 'All apps'], [0, 'Only selected apps']],
141
        $usub->manage_all, true
142
    );
143
    form_select2_multi(
144
        '', 'manage_apps', $apps, $manage, "id=manage_apps"
145
    );
146
    form_input_text('Quota', 'quota', $usub->quota);
147
    form_input_text('Max jobs in progress', 'max_jobs_in_progress', $usub->max_jobs_in_progress);
148
    form_submit('Update');
149
    form_end();
150
151
    // disable the app selector if 'All apps' checked
152
    //
153
    echo "
154
<script>
155
var select_apps = document.getElementById('submit_apps');
156
var submit_all_0 = document.getElementById('submit_all_0');
157
var submit_all_1 = document.getElementById('submit_all_1');
158
fsubmit = function() {
159
    select_apps.disabled = submit_all_1.checked;
160
};
161
fsubmit();
162
submit_all_0.onchange = fsubmit;
163
submit_all_1.onchange = fsubmit;
164
165
var manage_apps = document.getElementById('manage_apps');
166
var manage_all_0 = document.getElementById('manage_all_0');
167
var manage_all_1 = document.getElementById('manage_all_1');
168
fmanage = function() {
169
    manage_apps.disabled = manage_all_1.checked;
170
};
171
fmanage();
172
manage_all_0.onchange = fmanage;
173
manage_all_1.onchange = fmanage;
174
175
</script>
176
    ";
177
    page_tail();
178
}
179
180
function handle_edit_action() {
181
    $user_id = get_int('user_id');
182
    $us = BoincUserSubmit::lookup_userid($user_id);
183
    if (!$us) error_page("user not found");
184
    BoincUserSubmitApp::delete_user($user_id);
185
    if (get_str('submit_all')) {
186
        $us->update("submit_all=1");
187
    } else {
188
        $us->update("submit_all=0");
189
    }
190
    if (get_str('manage_all')) {
191
        $us->update("manage_all=1");
192
    } else {
193
        $us->update("manage_all=0");
194
    }
195
    $apps = BoincApp::enum("deprecated=0");
196
    $submit_apps = get_array('submit_apps');
197
    $submit_apps = array_map('intval', $submit_apps);
198
    $manage_apps = get_array('manage_apps');
199
    $manage_apps = array_map('intval', $manage_apps);
200
    foreach ($apps as $app) {
201
        $s = in_array($app->id, $submit_apps);
202
        $m = in_array($app->id, $manage_apps)?1:0;
203
        if ($s || $m) {
204
            BoincUserSubmitApp::insert(
205
                "(user_id, app_id, manage) values ($user_id, $app->id, $m)"
206
            );
207
        }
208
    }
209
    $quota = (double) get_str('quota');
210
    if ($quota != $us->quota) {
211
        $us->update("quota=$quota");
212
    }
213
    $mj = (int) get_str('max_jobs_in_progress');
214
    if ($mj != $us->max_jobs_in_progress) {
215
        $us->update("max_jobs_in_progress=$mj");
216
    }
217
    header('Location: manage_project.php');
218
}
219
220
function handle_add_form() {
221
    page_head("Add user");
222
    echo "
223
        <form action=manage_project.php>
224
        <input type=hidden name=action value=add_action>
225
        User ID: <input name=user_id>
226
        <br>
227
        <input class=\"btn btn-success\" type=submit value=OK>
228
        </form>
229
    ";
230
    page_tail();
231
}
232
233
function handle_add_action() {
234
    $user_id = get_int('user_id');
235
    $user = BoincUser::lookup_id($user_id);
236
    if (!$user) error_page("no such user");
237
    $us = BoincUserSubmit::lookup_userid($user_id);
238
    if (!$us) {
239
        if (!BoincUserSubmit::insert("(user_id, quota, logical_start_time, submit_all, manage_all, max_jobs_in_progress) values ($user_id, 0, 0, 0, 0, 0)")) {
240
            error_page("Insert failed");
241
        }
242
    }
243
    header("Location: manage_project.php?action=edit_form&user_id=$user_id");
244
}
245
246
$user = get_logged_in_user();
0 ignored issues
show
Are you sure the assignment to $user is correct as get_logged_in_user() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
247
$bus = BoincUserSubmit::lookup_userid($user->id);
248
if (!$bus) {
249
    error_page("no access");
250
}
251
252
$action = get_str('action', true);
253
switch ($action) {
254
case 'list':
255
case '':
256
    handle_list(); break;
257
case 'add_form':
258
    handle_add_form(); break;
259
case 'add_action':
260
    handle_add_action(); break;
261
case 'edit_form':
262
    handle_edit_form(); break;
263
case 'edit_action':
264
    handle_edit_action(); break;
265
default:
0 ignored issues
show
DEFAULT keyword must be indented 4 spaces from SWITCH keyword
Loading history...
DEFAULT case must have a breaking statement
Loading history...
266
    error_page("unknown action");
267
}
268
269
?>
270