forked from projectsend/projectsend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroles-edit.php
More file actions
273 lines (237 loc) · 11.1 KB
/
roles-edit.php
File metadata and controls
273 lines (237 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
/**
* Edit an existing user role
* Only accessible to Super Administrators (level 9)
*/
require_once 'bootstrap.php';
check_access_enhanced(['edit_settings']);
$active_nav = 'roles';
// Get role from URL
if (empty($_GET['role'])) {
$flash->error(__('No role specified.', 'cftp_admin'));
ps_redirect('roles.php');
}
$role_id = (int)$_GET['role'];
$role = new \ProjectSend\Classes\Roles($role_id);
if (!$role->exists()) {
$flash->error(__('Role not found.', 'cftp_admin'));
ps_redirect('roles.php');
}
$page_title = sprintf(__('Edit Role: %s', 'cftp_admin'), $role->name);
$page_id = 'roles_edit';
// Check if this is a system role and handle different editing levels
$is_client_role = ($role->name === 'Client');
$is_system_role = $role->is_system_role;
// Different editing permissions for different role types
if ($is_client_role) {
// Client role is completely read-only
$view_only = true;
$can_edit_name = false;
} elseif ($is_system_role) {
// Other system roles can edit description/status but not name
$view_only = false;
$can_edit_name = false;
} else {
// Custom roles are fully editable
$view_only = false;
$can_edit_name = true;
}
// Process form submission
if ($_POST && !$is_client_role) {
$validation_errors = [];
// Validate required fields (only for custom roles that can change name)
if ($can_edit_name && empty($_POST['name'])) {
$validation_errors[] = __('Role name is required.', 'cftp_admin');
}
// Prevent name changes for system roles
if (!$can_edit_name && isset($_POST['name']) && $_POST['name'] !== $role->name) {
$validation_errors[] = __('System role names cannot be changed.', 'cftp_admin');
}
if (empty($validation_errors)) {
$role_data = [
'description' => $_POST['description'] ?? '',
'active' => isset($_POST['active']) ? 1 : 0
];
// Only include name for custom roles
if ($can_edit_name) {
$role_data['name'] = $_POST['name'];
}
// Role level changes removed - roles use auto-generated IDs
$result = $role->update($role_data);
if ($result) {
$flash->success(__('Role updated successfully.', 'cftp_admin'));
// Redirect to updated role using role ID
ps_redirect('roles-edit.php?role=' . $role->id);
} else {
$flash->error(__('Could not update role. Please try again.', 'cftp_admin'));
}
} else {
foreach ($validation_errors as $error) {
$flash->error($error);
}
}
}
// Role levels are no longer used - roles use auto-generated IDs
// Get user count for this role
$user_count = $role->getUserCount();
// Header buttons
$header_action_buttons = [
[
'url' => 'role-permissions.php?role='.$role->id,
'label' => __('Manage permissions', 'cftp_admin'),
'icon' => 'fa fa-key'
],
[
'url' => 'roles.php',
'label' => __('Back to Roles', 'cftp_admin'),
'type' => 'light',
'icon' => 'fa fa-arrow-left',
],
];
include_once ADMIN_VIEWS_DIR . DS . 'header.php';
?>
<div class="row">
<div class="col-12 col-lg-8">
<div class="ps-card">
<div class="ps-card-body">
<h5>
<?php _e('Role Information', 'cftp_admin'); ?>
<?php if ($role->is_system_role): ?>
<span class="badge bg-primary ms-2"><?php _e('System Role', 'cftp_admin'); ?></span>
<?php endif; ?>
</h5>
<?php if ($is_system_role): ?>
<div class="alert alert-info">
<i class="fa fa-info-circle"></i>
<?php if ($is_client_role): ?>
<?php _e('The Client role is a core system role and cannot be edited.', 'cftp_admin'); ?>
<?php else: ?>
<?php _e('This is a system role. The role name cannot be changed as it may break system functionality.', 'cftp_admin'); ?>
<?php endif; ?>
</div>
<?php endif; ?>
<?php if ($is_client_role): ?>
<!-- View-only mode for Client role -->
<div class="form-group row">
<label class="col-sm-3 control-label"><?php _e('Role Name', 'cftp_admin'); ?></label>
<div class="col-sm-9">
<p class="form-control-static"><?php echo html_output($role->name); ?></p>
</div>
</div>
<div class="form-group row">
<label class="col-sm-3 control-label"><?php _e('Description', 'cftp_admin'); ?></label>
<div class="col-sm-9">
<p class="form-control-static"><?php echo html_output($role->description); ?></p>
</div>
</div>
<div class="form-group row">
<label class="col-sm-3 control-label"><?php _e('Status', 'cftp_admin'); ?></label>
<div class="col-sm-9">
<p class="form-control-static">
<?php if ($role->active): ?>
<span class="badge bg-success"><?php _e('Active', 'cftp_admin'); ?></span>
<?php else: ?>
<span class="badge bg-secondary"><?php _e('Inactive', 'cftp_admin'); ?></span>
<?php endif; ?>
</p>
</div>
</div>
<?php else: ?>
<!-- Editable form for other roles -->
<form action="" method="post" class="form-horizontal" id="role_form">
<?php addCsrf(); ?>
<div class="form-group row">
<label for="name" class="col-sm-3 control-label"><?php _e('Role Name', 'cftp_admin'); ?></label>
<div class="col-sm-9">
<input type="text" name="name" id="name" class="form-control required"
value="<?php echo html_output($role->name); ?>"
maxlength="255" required />
</div>
</div>
<div class="form-group row">
<label for="description" class="col-sm-3 control-label"><?php _e('Description', 'cftp_admin'); ?></label>
<div class="col-sm-9">
<textarea name="description" id="description" class="form-control" rows="3"><?php echo html_output($role->description); ?></textarea>
</div>
</div>
<!-- Role level fields removed - roles now use auto-generated IDs -->
<div class="form-group row">
<div class="col-sm-9 offset-sm-3">
<div class="form-check">
<input type="checkbox" name="active" id="active" class="form-check-input" value="1"
<?php echo $role->active ? 'checked' : ''; ?> />
<label for="active" class="form-check-label"><?php _e('Active', 'cftp_admin'); ?></label>
<small class="form-text text-muted"><?php _e('Inactive roles cannot be assigned to users', 'cftp_admin'); ?></small>
</div>
</div>
</div>
<div class="form-group row">
<div class="col-sm-12">
<button type="submit" class="btn btn-primary">
<i class="fa fa-check"></i> <?php _e('Update Role', 'cftp_admin'); ?>
</button>
<a href="roles.php" class="btn btn-secondary">
<?php _e('Cancel', 'cftp_admin'); ?>
</a>
</div>
</div>
</form>
<?php endif; ?>
</div>
</div>
</div>
<div class="col-12 col-lg-4">
<div class="ps-card">
<div class="ps-card-body">
<h5><?php _e('Role Statistics', 'cftp_admin'); ?></h5>
<div class="row text-center">
<div class="col-6">
<h2 class="text-primary"><?php echo $user_count; ?></h2>
<p class="text-muted mb-0"><?php _e('Users', 'cftp_admin'); ?></p>
</div>
<div class="col-6">
<h2 class="text-info"><?php echo count($role->permissions); ?></h2>
<p class="text-muted mb-0"><?php _e('Permissions', 'cftp_admin'); ?></p>
</div>
</div>
<?php if ($user_count > 0): ?>
<hr>
<?php if ($is_client_role): ?>
<a href="clients.php" class="btn btn-sm btn-primary">
<i class="fa fa-users"></i> <?php _e('View Clients', 'cftp_admin'); ?>
</a>
<?php else: ?>
<a href="users.php?role=<?php echo $role->id; ?>" class="btn btn-sm btn-primary">
<i class="fa fa-users"></i> <?php _e('View Users with this Role', 'cftp_admin'); ?>
</a>
<?php endif; ?>
<?php endif; ?>
</div>
</div>
<?php if (!$role->is_system_role && $user_count == 0): ?>
<div class="ps-card mt-3">
<div class="ps-card-body">
<h5><?php _e('Quick Actions', 'cftp_admin'); ?></h5>
<div class="d-grid gap-2">
<?php if (!$role->is_system_role && $user_count == 0): ?>
<button type="button" class="btn btn-danger" id="delete-role">
<i class="fa fa-trash"></i> <?php _e('Delete Role', 'cftp_admin'); ?>
</button>
<?php endif; ?>
</div>
</div>
</div>
<?php endif; ?>
<?php if ($role->is_system_role): ?>
<div class="ps-card mt-3">
<div class="ps-card-body">
<h5><?php _e('System Role Info', 'cftp_admin'); ?></h5>
<p class="text-muted mb-0"><?php _e('System roles are built into ProjectSend and provide core functionality. They cannot be deleted and have limited editability.', 'cftp_admin'); ?></p>
</div>
</div>
<?php endif; ?>
</div>
</div>
<?php
include_once ADMIN_VIEWS_DIR . DS . 'footer.php';
?>