get_app_lists()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 1
dl 0
loc 16
rs 9.8666
c 0
b 0
f 0
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',
148
        'max_jobs_in_progress', $usub->max_jobs_in_progress
149
    );
150
    form_general('Run jobs only on own computers?',
151
        $user->seti_id?'yes':'no'
152
    );
153
    form_submit('Update');
154
    form_end();
155
156
    // disable the app selector if 'All apps' checked
157
    //
158
    echo "
159
<script>
160
var select_apps = document.getElementById('submit_apps');
161
var submit_all_0 = document.getElementById('submit_all_0');
162
var submit_all_1 = document.getElementById('submit_all_1');
163
fsubmit = function() {
164
    select_apps.disabled = submit_all_1.checked;
165
};
166
fsubmit();
167
submit_all_0.onchange = fsubmit;
168
submit_all_1.onchange = fsubmit;
169
170
var manage_apps = document.getElementById('manage_apps');
171
var manage_all_0 = document.getElementById('manage_all_0');
172
var manage_all_1 = document.getElementById('manage_all_1');
173
fmanage = function() {
174
    manage_apps.disabled = manage_all_1.checked;
175
};
176
fmanage();
177
manage_all_0.onchange = fmanage;
178
manage_all_1.onchange = fmanage;
179
180
</script>
181
    ";
182
    page_tail();
183
}
184
185
function handle_edit_action() {
186
    $user_id = get_int('user_id');
187
    $us = BoincUserSubmit::lookup_userid($user_id);
188
    if (!$us) error_page("user not found");
189
    BoincUserSubmitApp::delete_user($user_id);
190
    if (get_str('submit_all')) {
191
        $us->update("submit_all=1");
192
    } else {
193
        $us->update("submit_all=0");
194
    }
195
    if (get_str('manage_all')) {
196
        $us->update("manage_all=1");
197
    } else {
198
        $us->update("manage_all=0");
199
    }
200
    $apps = BoincApp::enum("deprecated=0");
201
    $submit_apps = get_array('submit_apps');
202
    $submit_apps = array_map('intval', $submit_apps);
203
    $manage_apps = get_array('manage_apps');
204
    $manage_apps = array_map('intval', $manage_apps);
205
    foreach ($apps as $app) {
206
        $s = in_array($app->id, $submit_apps);
207
        $m = in_array($app->id, $manage_apps)?1:0;
208
        if ($s || $m) {
209
            BoincUserSubmitApp::insert(
210
                "(user_id, app_id, manage) values ($user_id, $app->id, $m)"
211
            );
212
        }
213
    }
214
    $quota = (double) get_str('quota');
215
    if ($quota != $us->quota) {
216
        $us->update("quota=$quota");
217
    }
218
    $mj = (int) get_str('max_jobs_in_progress');
219
    if ($mj != $us->max_jobs_in_progress) {
220
        $us->update("max_jobs_in_progress=$mj");
221
    }
222
    header('Location: manage_project.php');
223
}
224
225
function handle_add_form() {
226
    page_head("Add user");
227
    echo "
228
        <form action=manage_project.php>
229
        <input type=hidden name=action value=add_action>
230
        User ID: <input name=user_id>
231
        <br>
232
        <input class=\"btn btn-success\" type=submit value=OK>
233
        </form>
234
    ";
235
    page_tail();
236
}
237
238
function handle_add_action() {
239
    $user_id = get_int('user_id');
240
    $user = BoincUser::lookup_id($user_id);
241
    if (!$user) error_page("no such user");
242
    $us = BoincUserSubmit::lookup_userid($user_id);
243
    if (!$us) {
244
        if (!BoincUserSubmit::insert("(user_id, quota, logical_start_time, submit_all, manage_all, max_jobs_in_progress) values ($user_id, 1, 0, 0, 0, 10000)")) {
245
            error_page("Insert failed");
246
        }
247
    }
248
    header("Location: manage_project.php?action=edit_form&user_id=$user_id");
249
}
250
251
$user = get_logged_in_user();
0 ignored issues
show
Bug introduced by
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...
252
$bus = BoincUserSubmit::lookup_userid($user->id);
253
if (!$bus) {
254
    error_page("no access");
255
}
256
257
$action = get_str('action', true);
258
switch ($action) {
259
case 'list':
260
case '':
261
    handle_list(); break;
262
case 'add_form':
263
    handle_add_form(); break;
264
case 'add_action':
265
    handle_add_action(); break;
266
case 'edit_form':
267
    handle_edit_form(); break;
268
case 'edit_action':
269
    handle_edit_action(); break;
270
default:
0 ignored issues
show
Coding Style introduced by
DEFAULT keyword must be indented 4 spaces from SWITCH keyword
Loading history...
Coding Style introduced by
DEFAULT case must have a breaking statement
Loading history...
271
    error_page("unknown action");
272
}
273
274
?>
275