Passed
Push — dpa_submit24 ( 53eee7 )
by David
10:09
created

user_row()   B

Complexity

Conditions 9
Paths 4

Size

Total Lines 36
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 9
eloc 28
c 1
b 1
f 0
nc 4
nop 1
dl 0
loc 36
rs 8.0555
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
27
function user_row($u) {
28
    $user = BoincUser::lookup_id($u->user_id);
29
    $uas = BoincUserSubmitApp::enum("user_id=$u->user_id");
30
31
    if ($u->submit_all) {
32
        $sub = 'All applications';
33
    } else {
34
        $names = [];
35
        foreach ($uas as $ua) {
36
            $app = BoincApp::lookup_id($ua->app_id);
37
            $names[] = $app->name;
38
        }
39
        $sub = $names?implode(', ', $names):'---';
40
    }
41
    if ($u->manage_all) {
42
        $admin = 'All applications';
43
    } else {
44
        $names = [];
45
        foreach ($uas as $ua) {
46
            if (!$ua->manage) continue;
47
            $app = BoincApp::lookup_id($ua->app_id);
48
            $names[] = $app->name;
49
        }
50
        $admin = $names?implode(', ', $names):'---';
51
    }
52
53
    table_row(
54
        sprintf(
55
            '<a href=manage_project.php?action=edit_form&user_id=%d>%s</a>(ID: %d)',
56
            $u->user_id, $user->name, $user->id
57
        ),
58
        $sub,
59
        $admin,
60
        $u->quota,
61
        $u->max_jobs_in_progress,
62
        ($u->logical_start_time > time())?local_time_str($u->logical_start_time):'---'
63
    );
64
}
65
66
function handle_list() {
67
    page_head("Job submission access control");
68
    echo "The following users are allowed to submit jobs.
69
        <p>
70
    ";
71
72
    $us = BoincUserSubmit::enum("");
73
    start_table('table-striped');
74
    table_header(
75
        "User<br><small>Click to change permissions or quota</small>",
76
        "Can submit jobs to",
77
        "Can administer apps for",
78
        "Quota",
79
        "Max jobs in progress<br><small>0 means no limit</small>",
80
        "Current priority<br><small>Later time = lower priority</small>"
81
    );
82
    foreach ($us as $u) {
83
        user_row($u);
84
    }
85
    end_table();
86
    show_button("manage_project.php?action=add_form",
87
        "Add user", "Allow a new user to submit jobs"
88
    );
89
    page_tail();
90
}
91
92
// get multi-select lists for apps
93
//
94
function get_app_lists($user_id) {
95
    $items = [];
96
    $submit = [];
97
    $manage = [];
98
    $apps = BoincApp::enum("deprecated=0");
99
    foreach ($apps as $app) {
100
        $items[] = [$app->id, $app->name];
101
        $us = BoincUserSubmitApp::lookup("user_id=$user_id and app_id=$app->id");
102
        if ($us) {
103
            $submit[] = $app->id;
104
            if ($us->manage) {
105
                $manage[] = $app->id;
106
            }
107
        }
108
    }
109
    return [$items, $submit, $manage];
110
}
111
112
function handle_edit_form() {
113
    $user_id = get_int('user_id');
114
    $user = BoincUser::lookup_id($user_id);
115
    $usub = BoincUserSubmit::lookup_userid($user_id);
116
    page_head_select2("Job submission permissions for $user->name");
117
    form_start('manage_project.php');
118
    form_input_hidden('action', 'edit_action');
119
    form_input_hidden('user_id', $user->id);
120
    [$apps, $submit, $manage] = get_app_lists($user_id);
121
    form_radio_buttons('Can submit jobs to', 'submit_all',
122
        [[1, 'All apps'], [0, 'Only selected apps']],
123
        $usub->submit_all, true
124
    );
125
    form_select2_multi(
126
        '', 'submit_apps', $apps, $submit, "id=submit_apps"
127
    );
128
    form_radio_buttons('Can administer', 'manage_all',
129
        [[1, 'All apps'], [0, 'Only selected apps']],
130
        $usub->manage_all, true
131
    );
132
    form_select2_multi(
133
        '', 'manage_apps', $apps, $manage, "id=manage_apps"
134
    );
135
    form_input_text('Quota', 'quota', $usub->quota);
136
    form_input_text('Max jobs in progress', 'max_jobs_in_progress', $usub->max_jobs_in_progress);
137
    form_submit('Update');
138
    form_end();
139
140
    // disable the app selector if 'All apps' checked
141
    //
142
    echo "
143
<script>
144
var select_apps = document.getElementById('submit_apps');
145
var submit_all_0 = document.getElementById('submit_all_0');
146
var submit_all_1 = document.getElementById('submit_all_1');
147
fsubmit = function() {
148
    select_apps.disabled = submit_all_1.checked;
149
};
150
fsubmit();
151
submit_all_0.onchange = fsubmit;
152
submit_all_1.onchange = fsubmit;
153
154
var manage_apps = document.getElementById('manage_apps');
155
var manage_all_0 = document.getElementById('manage_all_0');
156
var manage_all_1 = document.getElementById('manage_all_1');
157
fmanage = function() {
158
    manage_apps.disabled = manage_all_1.checked;
159
};
160
fmanage();
161
manage_all_0.onchange = fmanage;
162
manage_all_1.onchange = fmanage;
163
164
</script>
165
    ";
166
    page_tail();
167
}
168
169
function handle_edit_action() {
170
    $user_id = get_int('user_id');
171
    $us = BoincUserSubmit::lookup_userid($user_id);
172
    if (!$us) error_page("user not found");
173
    BoincUserSubmitApp::delete_user($user_id);
174
    $submit_all = get_str('submit_all');
175
    if ($submit_all) {
176
        $us->update("submit_all=1");
177
    } else {
178
        $us->update("submit_all=0");
179
    }
180
    $apps = BoincApp::enum("deprecated=0");
181
    $submit_apps = get_array('submit_apps');
182
    $submit_apps = array_map('intval', $submit_apps);
183
    $manage_apps = get_array('manage_apps');
184
    $manage_apps = array_map('intval', $manage_apps);
185
    foreach ($apps as $app) {
186
        $s = in_array($app->id, $submit_apps);
187
        $m = in_array($app->id, $manage_apps)?1:0;
188
        if ($s || $m) {
189
            BoincUserSubmitApp::insert(
190
                "(user_id, app_id, manage) values ($user_id, $app->id, $m)"
191
            );
192
        }
193
    }
194
    $quota = (double) get_str('quota');
195
    if ($quota != $us->quota) {
196
        $us->update("quota=$quota");
197
    }
198
    $mj = (int) get_str('max_jobs_in_progress');
199
    if ($mj != $us->max_jobs_in_progress) {
200
        $us->update("max_jobs_in_progress=$mj");
201
    }
202
    header('Location: manage_project.php');
203
}
204
205
function handle_add_form() {
206
    page_head("Add user");
207
    echo "
208
        <form action=manage_project.php>
209
        <input type=hidden name=action value=add_action>
210
        User ID: <input name=user_id>
211
        <br>
212
        <input class=\"btn btn-success\" type=submit value=OK>
213
        </form>
214
    ";
215
    page_tail();
216
}
217
218
function handle_add_action() {
219
    $user_id = get_int('user_id');
220
    $user = BoincUser::lookup_id($user_id);
221
    if (!$user) error_page("no such user");
222
    $us = BoincUserSubmit::lookup_userid($user_id);
223
    if (!$us) {
224
        if (!BoincUserSubmit::insert("(user_id) values ($user_id)")) {
225
            error_page("Insert failed");
226
        }
227
    }
228
    header("Location: manage_project.php?action=edit_form&user_id=$user_id");
229
}
230
231
$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...
232
$bus = BoincUserSubmit::lookup_userid($user->id);
233
if (!$bus) {
234
    error_page("no access");
235
}
236
237
$action = get_str('action', true);
238
switch ($action) {
239
case 'list':
240
case '':
241
    handle_list(); break;
242
case 'add_form':
243
    handle_add_form(); break;
244
case 'add_action':
245
    handle_add_action(); break;
246
case 'edit_form':
247
    handle_edit_form(); break;
248
case 'edit_action':
249
    handle_edit_action(); break;
250
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...
251
    error_page("unknown action");
252
}
253
254
?>
255