Passed
Push — dpa_web21 ( c18d53 )
by David
09:16
created

get_app_lists()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 12
rs 9.9666
c 1
b 1
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
27
function user_row($u) {
28
    $user = BoincUser::lookup_id($u->user_id);
29
    echo "
30
        <tr>
31
        <td>
32
        <a href=manage_project.php?action=edit_form&user_id=$u->user_id>$user->name</a>
33
        (ID: $user->id)
34
        </td>
35
    ";
36
    echo "<td>";
37
    if ($u->submit_all) {
38
        echo "All applications\n";
39
    } else {
40
        $uas = BoincUserSubmitApp::enum("user_id=$u->user_id");
41
        $names = [];
42
        foreach ($uas as $ua) {
43
            $app = BoincApp::lookup_id($ua->app_id);
44
            $names[] = $app->name;
45
        }
46
        if (count($uas) == 0) {
47
            echo "---";
48
        } else {
49
            echo implode(', ', $names);
50
        }
51
    }
52
    echo "</td>\n";
53
    echo "<td>$u->quota</td>\n";
54
    echo "<td>$u->max_jobs_in_progress</td>\n";
55
    echo "<td>";
56
    if ($u->logical_start_time > time()) {
57
        echo local_time_str($u->logical_start_time);
58
    } else {
59
        echo "---";
60
    }
61
    echo "
62
        </td>
63
        </tr>
64
    ";
65
}
66
67
function handle_list() {
68
    page_head("Job submission access control");
69
    echo "The following users are allowed to submit jobs.
70
        <p>
71
    ";
72
73
    $us = BoincUserSubmit::enum("");
74
    start_table();
75
    table_header(
76
        "User<br><small>Click to change permissions or quota</small>",
77
        "Can submit jobs 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
    $selected = [];
97
    $apps = BoincApp::enum("deprecated=0");
98
    foreach ($apps as $app) {
99
        $items[] = [$app->id, $app->name];
100
        $us = BoincUserSubmitApp::lookup("user_id=$user_id and app_id=$app->id");
101
        if ($us) {
102
            $selected[] = $app->id;
103
        }
104
    }
105
    return [$items, $selected];
106
}
107
108
function handle_edit_form() {
109
    $user_id = get_int('user_id');
110
    $user = BoincUser::lookup_id($user_id);
111
    $usub = BoincUserSubmit::lookup_userid($user_id);
112
    page_head_select2("Job submission permissions for $user->name");
113
    form_start('manage_project.php');
114
    form_input_hidden('action', 'edit_action');
115
    form_input_hidden('user_id', $user->id);
116
    form_radio_buttons('Can submit jobs for', 'submit_all',
117
        [[1, 'All apps'], [0, 'Only selected apps']],
118
        $usub->submit_all, true
119
    );
120
    [$apps, $selected_apps] = get_app_lists($user_id);
121
    form_select2_multi(
122
        'Select apps', 'selected_apps', $apps, $selected_apps, "id=select_apps"
123
    );
124
    form_input_text('Quota', 'quota', $usub->quota);
125
    form_input_text('Max jobs in progress', 'max_jobs_in_progress', $usub->max_jobs_in_progress);
126
    form_submit('Update');
127
    form_end();
128
129
    // disable the app selector if 'All apps' checked
130
    //
131
    echo "
132
<script>
133
var select_apps = document.getElementById('select_apps');
134
var submit_all_0 = document.getElementById('submit_all_0');
135
var submit_all_1 = document.getElementById('submit_all_1');
136
f = function() {
137
    select_apps.disabled = submit_all_1.checked;
138
};
139
f();
140
submit_all_0.onchange = f;
141
submit_all_1.onchange = f;
142
</script>
143
    ";
144
    page_tail();
145
}
146
147
function handle_edit_action() {
148
    $user_id = get_int('user_id');
149
    $us = BoincUserSubmit::lookup_userid($user_id);
150
    if (!$us) error_page("user not found");
151
    BoincUserSubmitApp::delete_user($user_id);
152
    $submit_all = get_str('submit_all');
153
    if ($submit_all) {
154
        $us->update("submit_all=1");
155
    } else {
156
        $us->update("submit_all=0");
157
        $apps = BoincApp::enum("deprecated=0");
158
        $selected_apps = get_str('selected_apps');
159
        foreach ($apps as $app) {
160
            if (in_array($app->id, $selected_apps)) {
161
                BoincUserSubmitApp::insert("(user_id, app_id) values ($user_id, $app->id)");
162
            }
163
        }
164
    }
165
    $quota = (double) get_str('quota');
166
    if ($quota != $us->quota) {
167
        $us->update("quota=$quota");
168
    }
169
    $mj = (int) get_str('max_jobs_in_progress');
170
    if ($mj != $us->max_jobs_in_progress) {
171
        $us->update("max_jobs_in_progress=$mj");
172
    }
173
    header('Location: manage_project.php');
174
}
175
176
function handle_add_form() {
177
    page_head("Add user");
178
    echo "
179
        <form action=manage_project.php>
180
        <input type=hidden name=action value=add_action>
181
        User ID: <input name=user_id>
182
        <br>
183
        <input class=\"btn btn-success\" type=submit value=OK>
184
        </form>
185
    ";
186
    page_tail();
187
}
188
189
function handle_add_action() {
190
    $user_id = get_int('user_id');
191
    $user = BoincUser::lookup_id($user_id);
192
    if (!$user) error_page("no such user");
193
    $us = BoincUserSubmit::lookup_userid($user_id);
194
    if (!$us) {
195
        if (!BoincUserSubmit::insert("(user_id) values ($user_id)")) {
196
            error_page("Insert failed");
197
        }
198
    }
199
    header("Location: manage_project.php?action=edit_form&user_id=$user_id");
200
}
201
202
$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...
203
$bus = BoincUserSubmit::lookup_userid($user->id);
204
if (!$bus) {
205
    error_page("no access");
206
}
207
208
$action = get_str('action', true);
209
switch ($action) {
210
case 'list':
211
case '':
212
    handle_list(); break;
213
case 'add_form':
214
    handle_add_form(); break;
215
case 'add_action':
216
    handle_add_action(); break;
217
case 'edit_form':
218
    handle_edit_form(); break;
219
case 'edit_action':
220
    handle_edit_action(); break;
221
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...
222
    error_page("unknown action");
223
}
224
225
?>
226