This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
/
users.php
279 lines (243 loc) · 7.78 KB
/
users.php
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
274
275
276
277
278
<?php defined('SYSPATH') OR die('No direct script access.');
/**
* @package Controller
* @author Kyle Treubig
* @copyright (c) 2010 Kyle Treubig
* @license MIT
*/
class Controller_Admin_Users extends Controller_Template_Admin {
/**
* Register controller as an admin controller
*/
public function before() {
parent::before();
$this->restrict('user', 'manage');
$this->add_menu('Users', array(
'create' => Route::get('admin_main')->uri(array('controller'=>'users', 'action'=>'new')),
'edit profile' => Route::get('admin_main')->uri(array('controller'=>'users', 'action'=>'profile')),
));
if ( ! $this->internal_request)
{
unset($this->template->menu->menu['Users'][0]);
}
}
/**
* Default action to list
*/
public function action_index() {
$this->action_list();
}
/**
* Display menu for user management
*/
public function action_menu() {
if ( ! $this->internal_request)
{
Request::instance()->redirect(Route::get('admin_main')->uri(array('controller'=>'users')));
}
$this->template->content = new View('admin/users/widget/menu');
}
/**
* Display list of users
*/
public function action_list() {
$users = Sprig::factory('user')->load(NULL, FALSE);
if (count($users) == 0)
{
$this->template->content = $this->internal_request
? new View('admin/users/widget/none')
: new View('admin/users/none');
return;
}
$grid = new Grid;
$grid->column()->field('id')->title('ID');
$grid->column('action')->title('Username')->display_field('username')
->action(Route::get('admin_main')->uri(array('controller'=>'users', 'action'=>'view')));
$grid->column()->field('role')->title('Role');
$grid->column()->field('email')->title('Email');
$grid->column('action')->title('Actions')->text('Edit')->class('edit')
->action(Route::get('admin_main')->uri(array('controller'=>'users', 'action'=>'edit')));
$grid->column('action')->title('')->text('Delete')->class('delete')
->action(Route::get('admin_main')->uri(array('controller'=>'users', 'action'=>'delete')));
$grid->data($users);
$this->template->content = $this->internal_request
? new View('admin/users/widget/list')
: new View('admin/users/list');
$this->template->content->grid = $grid;
}
/**
* Display details for a user
*/
public function action_view() {
$id = Request::instance()->param('id');
$user = Sprig::factory('user', array('id'=>$id))->load();
// If user is invalid, return to list
if ( ! $user->loaded())
{
Message::instance()->error('That user does not exist.');
Request::instance()->redirect( Route::get('admin_main')->uri(array('controller'=>'users')) );
}
// Restrict access
if ( ! $this->a2->allowed($user, 'view'))
{
Message::instance()->error('You do not have permission to view :name\'s details.', array(':name'=>$user->username));
Request::instance()->redirect( Route::get('admin_main')->uri(array('controller'=>'users')) );
}
$view = $this->internal_request
? new View('admin/users/widget/view')
: new View('admin/users/view');
$view->user = $user;
$this->template->content = $view;
}
/**
* Create a new user
*/
public function action_new() {
// Restrict access
if ( ! $this->a2->allowed('user', 'create'))
{
Message::instance()->error('You do not have permission to create new users.');
Request::instance()->redirect( Route::get('admin_main')->uri(array('controller'=>'users')) );
}
$user = Sprig::factory('user')->values($_POST);
try
{
$user->create();
Message::instance()->info('The user, :name, has been created.', array(':name'=>$user->username));
Request::instance()->redirect( Route::get('admin_main')->uri(array('controller'=>'users')) );
}
catch (Validate_Exception $e)
{
$view = $this->internal_request
? new View('admin/users/widget/form')
: new View('admin/users/create');
$view->legend = __('Create User');
$view->submit = __('Create');
$view->user = $user;
$view->errors = count($_POST) ? $e->array->errors('admin') : array();
$this->template->content = $view;
}
}
/**
* Edit user details
*/
public function action_edit() {
$id = Request::instance()->param('id');
$user = Sprig::factory('user', array('id'=>$id))->load();
// If user is invalid, return to list
if ( ! $user->loaded())
{
Message::instance()->error('That user does not exist.');
Request::instance()->redirect( Route::get('admin_main')->uri(array('controller'=>'users')) );
}
// Restrict access
if ( ! $this->a2->allowed($user, 'edit'))
{
Message::instance()->error('You do not have permission to modify :name.', array(':name'=>$user->username));
Request::instance()->redirect( Route::get('admin_main')->uri(array('controller'=>'users')) );
}
// Restrict promotion (change in role)
if ( ! $this->a2->allowed($user, 'promote'))
{
isset($_POST['role']) ? $_POST['role'] = $user->role : NULL;
}
// Restrict renaming
if ( ! $this->a2->allowed($user, 'rename'))
{
isset($_POST['username']) ? $_POST['username'] = $user->username : NULL;
}
// Unset password if not changing it
if (empty($_POST['password']))
{
unset($_POST['password']);
unset($_POST['password_confirm']);
}
$user->values($_POST);
$view = $this->internal_request
? new View('admin/users/widget/form')
: new View('admin/users/edit');
$view->legend = __('Modify User');
$view->submit = __('Save');
$view->user = $user;
if (count($_POST))
{
try
{
$user->update();
Message::instance()->info('The user, :name, has been modified.', array(':name'=>$user->username));
Request::instance()->redirect( Route::get('admin_main')->uri(array('controller'=>'users')) );
}
catch (Validate_Exception $e)
{
$view->errors = count($_POST) ? $e->array->errors('admin') : array();
}
}
$this->template->content = $view;
}
/**
* Edit a user's own profile
*/
public function action_profile() {
$user = $this->a1->get_user();
if ($user !== FALSE)
{
Request::instance()->redirect( Route::get('admin_main')
->uri(array(
'controller' => 'users',
'action' => 'edit',
'id' => $user->id,
))
);
}
else
{
Message::instance()->error('You must be logged in to do that.');
Request::instance()->redirect( Route::get('admin_auth')
->uri(array('action'=>'login'))
);
}
}
/**
* Delete a user
*/
public function action_delete() {
if (isset($_POST['no']))
{
Request::instance()->redirect( Route::get('admin_main')->uri(array('controller'=>'users')) );
}
$id = Request::instance()->param('id');
$user = Sprig::factory('user', array('id'=>$id))->load();
// If user is invalid, return to list
if ( ! $user->loaded())
{
Message::instance()->error('That user does not exist.');
Request::instance()->redirect( Route::get('admin_main')->uri(array('controller'=>'users')) );
}
$name = $user->username;
// Restrict access
if ( ! $this->a2->allowed($user, 'delete'))
{
Message::instance()->error('You do not have permission to delete :name.', array(':name'=>$name));
Request::instance()->redirect( Route::get('admin_main')->uri(array('controller'=>'users')) );
}
if (isset($_POST['yes']))
{
try
{
$user->delete();
Message::instance()->info('The user, :name, has been deleted.', array(':name'=>$name));
Request::instance()->redirect( Route::get('admin_main')->uri(array('controller'=>'users')) );
}
catch (Exception $e)
{
Message::instance()->error('An error occured deleting user, :name.', array(':name'=>$name));
Request::instance()->redirect( Route::get('admin_main')->uri(array('controller'=>'users')) );
}
}
$view = $this->internal_request
? new View('admin/users/widget/delete')
: new View('admin/users/delete');
$view->user = $user;
$this->template->content = $view;
}
} // End of Controller_Admin_Users