PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/admin/AdminUsersController.php

https://gitlab.com/jnettome/myproject
PHP | 307 lines | 150 code | 50 blank | 107 comment | 14 complexity | b8f32b8eb3ecd9591263543173d726dc MD5 | raw file
  1. <?php
  2. class AdminUsersController extends AdminController {
  3. /**
  4. * User Model
  5. * @var User
  6. */
  7. protected $user;
  8. /**
  9. * Role Model
  10. * @var Role
  11. */
  12. protected $role;
  13. /**
  14. * Permission Model
  15. * @var Permission
  16. */
  17. protected $permission;
  18. /**
  19. * Inject the models.
  20. * @param User $user
  21. * @param Role $role
  22. * @param Permission $permission
  23. */
  24. public function __construct(User $user, Role $role, Permission $permission)
  25. {
  26. parent::__construct();
  27. $this->user = $user;
  28. $this->role = $role;
  29. $this->permission = $permission;
  30. }
  31. /**
  32. * Display a listing of the resource.
  33. *
  34. * @return Response
  35. */
  36. public function getIndex()
  37. {
  38. // Title
  39. $title = Lang::get('admin/users/title.user_management');
  40. // Grab all the users
  41. $users = $this->user;
  42. // Show the page
  43. return View::make('admin/users/index', compact('users', 'title'));
  44. }
  45. /**
  46. * Show the form for creating a new resource.
  47. *
  48. * @return Response
  49. */
  50. public function getCreate()
  51. {
  52. // All roles
  53. $roles = $this->role->all();
  54. // Get all the available permissions
  55. $permissions = $this->permission->all();
  56. // Selected groups
  57. $selectedRoles = Input::old('roles', array());
  58. // Selected permissions
  59. $selectedPermissions = Input::old('permissions', array());
  60. // Title
  61. $title = Lang::get('admin/users/title.create_a_new_user');
  62. // Mode
  63. $mode = 'create';
  64. // Show the page
  65. return View::make('admin/users/create_edit', compact('roles', 'permissions', 'selectedRoles', 'selectedPermissions', 'title', 'mode'));
  66. }
  67. /**
  68. * Store a newly created resource in storage.
  69. *
  70. * @return Response
  71. */
  72. public function postCreate()
  73. {
  74. $this->user->username = Input::get( 'username' );
  75. $this->user->email = Input::get( 'email' );
  76. $this->user->password = Input::get( 'password' );
  77. // The password confirmation will be removed from model
  78. // before saving. This field will be used in Ardent's
  79. // auto validation.
  80. $this->user->password_confirmation = Input::get( 'password_confirmation' );
  81. $this->user->confirmed = Input::get( 'confirm' );
  82. // Permissions are currently tied to roles. Can't do this yet.
  83. //$user->permissions = $user->roles()->preparePermissionsForSave(Input::get( 'permissions' ));
  84. // Save if valid. Password field will be hashed before save
  85. $this->user->save();
  86. if ( $this->user->id )
  87. {
  88. // Save roles. Handles updating.
  89. $this->user->saveRoles(Input::get( 'roles' ));
  90. // Redirect to the new user page
  91. return Redirect::to('admin/users/' . $this->user->id . '/edit')->with('success', Lang::get('admin/users/messages.create.success'));
  92. }
  93. else
  94. {
  95. // Get validation errors (see Ardent package)
  96. $error = $this->user->errors()->all();
  97. return Redirect::to('admin/users/create')
  98. ->withInput(Input::except('password'))
  99. ->with( 'error', $error );
  100. }
  101. }
  102. /**
  103. * Display the specified resource.
  104. *
  105. * @param $user
  106. * @return Response
  107. */
  108. public function getShow($user)
  109. {
  110. // redirect to the frontend
  111. }
  112. /**
  113. * Show the form for editing the specified resource.
  114. *
  115. * @param $user
  116. * @return Response
  117. */
  118. public function getEdit($user)
  119. {
  120. if ( $user->id )
  121. {
  122. $roles = $this->role->all();
  123. $permissions = $this->permission->all();
  124. // Title
  125. $title = Lang::get('admin/users/title.user_update');
  126. // mode
  127. $mode = 'edit';
  128. return View::make('admin/users/create_edit', compact('user', 'roles', 'permissions', 'title', 'mode'));
  129. }
  130. else
  131. {
  132. return Redirect::to('admin/users')->with('error', Lang::get('admin/users/messages.does_not_exist'));
  133. }
  134. }
  135. /**
  136. * Update the specified resource in storage.
  137. *
  138. * @param $user
  139. * @return Response
  140. */
  141. public function postEdit($user)
  142. {
  143. // Validate the inputs
  144. $validator = Validator::make(Input::all(), $user->getUpdateRules());
  145. if ($validator->passes())
  146. {
  147. $oldUser = clone $user;
  148. $user->username = Input::get( 'username' );
  149. $user->email = Input::get( 'email' );
  150. $user->confirmed = Input::get( 'confirm' );
  151. $password = Input::get( 'password' );
  152. $passwordConfirmation = Input::get( 'password_confirmation' );
  153. if(!empty($password)) {
  154. if($password === $passwordConfirmation) {
  155. $user->password = $password;
  156. // The password confirmation will be removed from model
  157. // before saving. This field will be used in Ardent's
  158. // auto validation.
  159. $user->password_confirmation = $passwordConfirmation;
  160. } else {
  161. // Redirect to the new user page
  162. return Redirect::to('admin/users/' . $user->id . '/edit')->with('error', Lang::get('admin/users/messages.password_does_not_match'));
  163. }
  164. } else {
  165. unset($user->password);
  166. unset($user->password_confirmation);
  167. }
  168. if($user->confirmed == null) {
  169. $user->confirmed = $oldUser->confirmed;
  170. }
  171. $user->prepareRules($oldUser, $user);
  172. // Save if valid. Password field will be hashed before save
  173. $user->amend();
  174. // Save roles. Handles updating.
  175. $user->saveRoles(Input::get( 'roles' ));
  176. } else {
  177. return Redirect::to('admin/users/' . $user->id . '/edit')->with('error', Lang::get('admin/users/messages.edit.error'));
  178. }
  179. // Get validation errors (see Ardent package)
  180. $error = $user->errors()->all();
  181. if(empty($error)) {
  182. // Redirect to the new user page
  183. return Redirect::to('admin/users/' . $user->id . '/edit')->with('success', Lang::get('admin/users/messages.edit.success'));
  184. } else {
  185. return Redirect::to('admin/users/' . $user->id . '/edit')->with('error', Lang::get('admin/users/messages.edit.error'));
  186. }
  187. }
  188. /**
  189. * Remove user page.
  190. *
  191. * @param $user
  192. * @return Response
  193. */
  194. public function getDelete($user)
  195. {
  196. // Title
  197. $title = Lang::get('admin/users/title.user_delete');
  198. // Show the page
  199. return View::make('admin/users/delete', compact('user', 'title'));
  200. }
  201. /**
  202. * Remove the specified user from storage.
  203. *
  204. * @param $user
  205. * @return Response
  206. */
  207. public function postDelete($user)
  208. {
  209. // Check if we are not trying to delete ourselves
  210. if ($user->id === Confide::user()->id)
  211. {
  212. // Redirect to the user management page
  213. return Redirect::to('admin/users')->with('error', Lang::get('admin/users/messages.delete.impossible'));
  214. }
  215. AssignedRoles::where('user_id', $user->id)->delete();
  216. $id = $user->id;
  217. $user->delete();
  218. // Was the comment post deleted?
  219. $user = User::find($id);
  220. if ( empty($user) )
  221. {
  222. // TODO needs to delete all of that user's content
  223. return Redirect::to('admin/users')->with('success', Lang::get('admin/users/messages.delete.success'));
  224. }
  225. else
  226. {
  227. // There was a problem deleting the user
  228. return Redirect::to('admin/users')->with('error', Lang::get('admin/users/messages.delete.error'));
  229. }
  230. }
  231. /**
  232. * Show a list of all the users formatted for Datatables.
  233. *
  234. * @return Datatables JSON
  235. */
  236. public function getData()
  237. {
  238. $users = User::leftjoin('assigned_roles', 'assigned_roles.user_id', '=', 'users.id')
  239. ->leftjoin('roles', 'roles.id', '=', 'assigned_roles.role_id')
  240. ->select(array('users.id', 'users.username','users.email', 'roles.name as rolename', 'users.confirmed', 'users.created_at'));
  241. return Datatables::of($users)
  242. // ->edit_column('created_at','{{{ Carbon::now()->diffForHumans(Carbon::createFromFormat(\'Y-m-d H\', $test)) }}}')
  243. ->edit_column('confirmed','@if($confirmed)
  244. Yes
  245. @else
  246. No
  247. @endif')
  248. ->add_column('actions', '<a href="{{{ URL::to(\'admin/users/\' . $id . \'/edit\' ) }}}" class="iframe btn btn-xs btn-default">{{{ Lang::get(\'button.edit\') }}}</a>
  249. @if($username == \'admin\')
  250. @else
  251. <a href="{{{ URL::to(\'admin/users/\' . $id . \'/delete\' ) }}}" class="iframe btn btn-xs btn-danger">{{{ Lang::get(\'button.delete\') }}}</a>
  252. @endif
  253. ')
  254. ->remove_column('id')
  255. ->make();
  256. }
  257. }