Passed
Push — client_release/8/8.2 ( 0798cc...523f15 )
by Vitalii
08:48
created

user_row()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 44
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 6
eloc 33
c 1
b 1
f 0
nc 6
nop 1
dl 0
loc 44
rs 8.7697
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
    echo "
32
        <tr>
33
        <td>
34
        <a href=manage_project.php?action=edit_form&user_id=$u->user_id>$user->name</a>
35
        (ID: $user->id)
36
        </td>
37
    ";
38
    echo "<td>";
39
    if ($u->submit_all) {
40
        echo "All applications\n";
41
    } else {
42
        $uas = BoincUserSubmitApp::enum("user_id=$u->user_id");
43
        $names = [];
44
        foreach ($uas as $ua) {
45
            $app = BoincApp::lookup_id($ua->app_id);
46
            $names[] = $app->name;
47
        }
48
        if (count($uas) == 0) {
49
            echo "---";
50
        } else {
51
            echo implode(', ', $names);
52
        }
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,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sub seems to be never defined.
Loading history...
68
        $admin,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $admin seems to be never defined.
Loading history...
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
    $selected = [];
108
    $apps = BoincApp::enum("deprecated=0");
109
    foreach ($apps as $app) {
110
        $items[] = [$app->id, $app->name];
111
        $us = BoincUserSubmitApp::lookup("user_id=$user_id and app_id=$app->id");
112
        if ($us) {
113
            $selected[] = $app->id;
114
        }
115
    }
116
    return [$items, $selected];
117
}
118
119
function handle_edit_form() {
120
    $user_id = get_int('user_id');
121
    $user = BoincUser::lookup_id($user_id);
122
    $usub = BoincUserSubmit::lookup_userid($user_id);
123
    page_head_select2("Job submission permissions for $user->name");
124
    form_start('manage_project.php');
125
    form_input_hidden('action', 'edit_action');
126
    form_input_hidden('user_id', $user->id);
127
    form_radio_buttons('Can submit jobs for', 'submit_all',
128
        [[1, 'All apps'], [0, 'Only selected apps']],
129
        $usub->submit_all, true
130
    );
131
    [$apps, $selected_apps] = get_app_lists($user_id);
132
    form_select2_multi(
133
        'Select apps', 'selected_apps', $apps, $selected_apps, "id=select_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('select_apps');
145
var submit_all_0 = document.getElementById('submit_all_0');
146
var submit_all_1 = document.getElementById('submit_all_1');
147
f = function() {
148
    select_apps.disabled = submit_all_1.checked;
149
};
150
f();
151
submit_all_0.onchange = f;
152
submit_all_1.onchange = f;
153
</script>
154
    ";
155
    page_tail();
156
}
157
158
function handle_edit_action() {
159
    $user_id = get_int('user_id');
160
    $us = BoincUserSubmit::lookup_userid($user_id);
161
    if (!$us) error_page("user not found");
162
    BoincUserSubmitApp::delete_user($user_id);
163
    $submit_all = get_str('submit_all');
164
    if ($submit_all) {
165
        $us->update("submit_all=1");
166
    } else {
167
        $us->update("submit_all=0");
168
        $apps = BoincApp::enum("deprecated=0");
169
        $selected_apps = get_str('selected_apps');
170
        foreach ($apps as $app) {
171
            if (in_array($app->id, $selected_apps)) {
172
                BoincUserSubmitApp::insert("(user_id, app_id) values ($user_id, $app->id)");
173
            }
174
        }
175
    }
176
    $quota = (double) get_str('quota');
177
    if ($quota != $us->quota) {
178
        $us->update("quota=$quota");
179
    }
180
    $mj = (int) get_str('max_jobs_in_progress');
181
    if ($mj != $us->max_jobs_in_progress) {
182
        $us->update("max_jobs_in_progress=$mj");
183
    }
184
    header('Location: manage_project.php');
185
}
186
187
function handle_add_form() {
188
    page_head("Add user");
189
    echo "
190
        <form action=manage_project.php>
191
        <input type=hidden name=action value=add_action>
192
        User ID: <input name=user_id>
193
        <br>
194
        <input class=\"btn btn-success\" type=submit value=OK>
195
        </form>
196
    ";
197
    page_tail();
198
}
199
200
function handle_add_action() {
201
    $user_id = get_int('user_id');
202
    $user = BoincUser::lookup_id($user_id);
203
    if (!$user) error_page("no such user");
204
    $us = BoincUserSubmit::lookup_userid($user_id);
205
    if (!$us) {
206
        if (!BoincUserSubmit::insert("(user_id) values ($user_id)")) {
207
            error_page("Insert failed");
208
        }
209
    }
210
    header("Location: manage_project.php?action=edit_form&user_id=$user_id");
211
}
212
213
$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...
214
$bus = BoincUserSubmit::lookup_userid($user->id);
215
if (!$bus) {
216
    error_page("no access");
217
}
218
219
$action = get_str('action', true);
220
switch ($action) {
221
case 'list':
222
case '':
223
    handle_list(); break;
224
case 'add_form':
225
    handle_add_form(); break;
226
case 'add_action':
227
    handle_add_action(); break;
228
case 'edit_form':
229
    handle_edit_form(); break;
230
case 'edit_action':
231
    handle_edit_action(); break;
232
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...
233
    error_page("unknown action");
234
}
235
236
?>
237