]>
Commit | Line | Data |
---|---|---|
4b48b46f VP |
1 | <?php |
2 | ||
c06c1713 BM |
3 | require_once "../inc/common.php"; |
4 | require_once "../inc/header.php"; | |
4b48b46f | 5 | |
088733d4 VP |
6 | if (!current_user_can(PERMISSION_MANAGE_USERS)) |
7 | { | |
8 | echo "<h4>Access denied</h4>"; | |
9 | die(); | |
10 | } | |
4b48b46f VP |
11 | $permissions = get_panel_user_permission_list(); |
12 | $list = get_panel_user_roles_list(); | |
088733d4 VP |
13 | |
14 | /** | |
15 | * Add a new role | |
16 | */ | |
17 | $errors = []; | |
18 | $success = []; | |
19 | ||
20 | ||
21 | ||
22 | if (isset($_POST['add_role_name']) && $role_name = $_POST['add_role_name']) | |
23 | { | |
24 | foreach ($list as $name => $u) // don't add it if it already exists | |
25 | { | |
26 | if (!strcmp(to_slug($name),to_slug($role_name))) | |
27 | { | |
28 | $errors[] = "Cannot create role \"$role_name\": A role with that name already exists."; | |
29 | break; | |
30 | } | |
31 | } | |
32 | if (empty($errors)) // so far so good | |
33 | { | |
34 | $msg = "Added user role \"$role_name\""; | |
35 | $permissions = []; | |
36 | if (isset($_POST['use_dup_role']) && $dup = $_POST['dup_role']) // if they're duplicating a role | |
37 | { | |
38 | $permissions = $list[$dup]; | |
39 | $msg .= ", a duplicate of \"$dup\""; | |
40 | } | |
41 | $settings = DbSettings::get(); | |
42 | $clean_perms = []; | |
43 | foreach($permissions as $k => $v) | |
44 | $clean_perms[] = $v; | |
45 | ||
46 | $settings['user_roles'][$role_name] = $clean_perms; | |
47 | DbSettings::set('user_roles', $settings['user_roles']); | |
48 | $success[] = $msg; | |
49 | $list = get_panel_user_roles_list(); // refresh | |
50 | ||
51 | } | |
52 | } | |
53 | ||
54 | elseif (isset($_POST['del_role_name']) && $role_name = $_POST['del_role_name']) | |
55 | { | |
56 | $found = 0; | |
57 | foreach ($list as $name => $u) // don't add it if it already exists | |
58 | { | |
59 | if (!strcmp(to_slug($name),to_slug($role_name))) | |
60 | { | |
61 | $found = 1; | |
62 | break; | |
63 | } | |
64 | } | |
65 | if ($found) // so far so good | |
66 | { | |
67 | $settings = DbSettings::get(); | |
68 | unset($settings['user_roles'][$role_name]); | |
69 | DbSettings::set('user_roles', $settings['user_roles']); | |
70 | $success[] = "Successfully deleted role \"$role_name\""; | |
71 | $list = get_panel_user_roles_list(); // refresh | |
72 | } | |
73 | else | |
74 | $errors[] = "Could not delete role \"$role_name\": Role does not exist."; | |
75 | } | |
4b48b46f VP |
76 | ?> |
77 | ||
4b48b46f | 78 | |
088733d4 | 79 | <div class="container-xxl row justify-content-between"> |
4b48b46f | 80 | |
088733d4 VP |
81 | <div class="col"> |
82 | <h4>User Role Editor</h4> | |
83 | <?php if (!empty($errors)) Message::Fail($errors); if (!empty($success)) Message::Success($success); ?> | |
84 | Roles are user categories where each has it's own set of permissions.<br> | |
85 | Here, you can easily add and edit User Roles to ensure that your team has the appropriate access and permissions they need.<br> | |
86 | Once you've created a role, you can assign it to a user on your panel, and they will have the permissions assigned to their role.<br><br> | |
87 | <div class="font-italic">Some roles are built-in and cannot be deleted or modified, specifically "<code>Super Admin</code>" and "<code>Read Only</code>"</div><br><br> | |
88 | Click a role name to view role permissions. | |
89 | </div> | |
90 | <div class="col" id="addnew_collapse"> | |
91 | <form method="post"> | |
92 | <div class="card card-body" style="max-width:550px"> | |
93 | <h5>Create New Role</h5> | |
94 | <div class="font-italic mb-3">You must create a new role before you can add permissions to it.</div> | |
95 | <div class="row input-group ml-0 mb-2"> | |
96 | <div class="input-group-prepend"> | |
97 | <span class="input-group-text" style="width:150px">New Role Name</span> | |
98 | </div> | |
99 | <input id="add_role_name" name="add_role_name" class="form-control" style="min-width:100px;max-width:450px" type="text"> | |
100 | ||
101 | ||
102 | </div> | |
103 | <div class="input-group"> | |
104 | <div class="input-group-prepend"> | |
105 | <div style="width:150px" class="input-group-text"> | |
106 | <input id="use_dup_role" name="use_dup_role" type="checkbox" class="mr-2">Duplicate Role | |
107 | </div> | |
108 | </div> | |
109 | <select name="dup_role" disabled class="custom-select" id="dup_role" style="min-width:100px;max-width:450px"> | |
110 | <option value="0" selected>None</option> | |
111 | <?php | |
112 | foreach($list as $s => $l) | |
113 | echo "<option value=\"$s\">$s</option>"; | |
114 | ?> | |
115 | </select> | |
116 | </div> | |
117 | <div class="mt-2 text-right"> | |
118 | <button type="submit" disabled id="role_submit" style="background-color:darkslateblue;color:white" class="btn btn-primary">Create Role</button> | |
119 | </div> | |
120 | ||
121 | </form> | |
122 | </div> | |
123 | </div> | |
124 | </div> | |
4b48b46f VP |
125 | <style> |
126 | ||
127 | #permlist #roles_accord .card .card-header .btn-header-link:after { | |
128 | content: "\f106"; | |
129 | font-family: 'Font Awesome 5 Free'; | |
130 | font-weight: 900; | |
131 | float: right; | |
132 | } | |
133 | ||
134 | #permlist #roles_accord .card .card-header .btn-header-link.collapsed:after { | |
135 | content: "\f107"; | |
136 | } | |
137 | ||
138 | </style> | |
088733d4 VP |
139 | |
140 | ||
141 | <script> | |
142 | const add_role_name = document.getElementById("add_role_name"); | |
143 | const use_dup = document.getElementById("use_dup_role"); | |
144 | const dup_role = document.getElementById("dup_role"); | |
145 | const role_submit = document.getElementById("role_submit"); | |
146 | ||
147 | use_dup.addEventListener('click', e => { | |
148 | if (use_dup.checked) { | |
149 | dup_role.disabled = false; | |
150 | } else { | |
151 | dup_role.value = "0"; | |
152 | dup_role.disabled = true; | |
153 | } | |
154 | }); | |
155 | ||
156 | add_role_name.addEventListener('input', e => { | |
157 | if (!add_role_name.value.length) | |
158 | role_submit.disabled = true; | |
159 | else | |
160 | role_submit.disabled = false; | |
161 | }); | |
162 | </script> | |
4b48b46f VP |
163 | <?php |
164 | ||
165 | generate_role_list($list); | |
166 | ||
088733d4 | 167 | |
c06c1713 | 168 | require_once "../inc/footer.php"; |