Guest User

Untitled

a guest
Jul 27th, 2016
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.37 KB | None | 0 0
  1. <?php namespace Kit\Http\Controllers\Admin;
  2.  
  3. use Kit\Http\Controllers\AdminController;
  4. use Cartalyst\Sentinel\Users\LoginRequiredException;
  5. use Cartalyst\Sentinel\Users\PasswordRequiredException;
  6. use Cartalyst\Sentinel\Users\UserExistsException;
  7. use Cartalyst\Sentinel\Users\UserNotFoundException;
  8. use Config;
  9. use Input;
  10. use Lang;
  11. use Redirect;
  12. use Sentinel;
  13. use Validator;
  14. use View;
  15. use App\User;
  16.  
  17. class UsersController extends AdminController
  18. {
  19.  
  20. /**
  21. * Declare the rules for the form validation
  22. *
  23. * @var array
  24. */
  25. protected $validationRules = array(
  26. 'first_name' => 'required|min:3',
  27. 'last_name' => 'required|min:3',
  28. 'email' => 'required|email|unique:users,email',
  29. 'password' => 'required|between:3,32',
  30. 'password_confirm' => 'required|between:3,32|same:password',
  31. );
  32.  
  33. /**
  34. * Show a list of all the users.
  35. *
  36. * @return View
  37. */
  38. public function getIndex()
  39. {
  40. // Grab all the users
  41. $users = Sentinel::getUserRepository()->createModel()->orderBy('users.created_at', 'DESC');
  42.  
  43. if (Input::get('type')) {
  44. if (Input::get('type') == 'onlyTrainee') {
  45. // $users->has('traineeProfile');
  46. // $users->join('users','trainer_profiles.user_id','=','users.id','left outer');
  47.  
  48. }
  49. elseif (Input::get('type') == 'onlyCorporateTrainee') {
  50. $users->join('corporate_profiles', 'users.id','=','corporate_profiles.trainee_profile_id');
  51. }
  52. elseif (Input::get('type') == 'onlyNotActivatedTrainers') {
  53. $users->join('activations','users.id','=','activations.user_id')
  54. ->where('completed', 0)
  55. ->orWhere('admin_approved', 0);
  56. }
  57. elseif (Input::get('type') == 'onlyTrainer') {
  58. $users->has('trainerProfile');
  59. }
  60. }
  61.  
  62. // Paginate the users
  63. $users = $users->with('activations','trainerProfile','traineeProfile')->get();
  64.  
  65. /*->appends(array(
  66. 'withTrashed' => Input::get('withTrashed'),
  67. 'onlyTrashed' => Input::get('onlyTrashed'),
  68. 'type' => Input::get('type'),
  69. ));*/
  70.  
  71. // Show the page
  72. return View::make('kit::backend.users.index', compact('users'));
  73. }
  74.  
  75. /**
  76. * User create.
  77. *
  78. * @return View
  79. */
  80. public function getCreate()
  81. {
  82. if (! Sentinel::getUser()->hasAccess('user.create')) {
  83. // Redirect to the user management page
  84. return Redirect::route('users')->with('error', 'Insufficient permissions!');
  85. }
  86.  
  87. // Get all the available groups
  88. $groups = Sentinel::getRoleRepository()->get();
  89.  
  90. // Selected groups
  91. $selectedGroups = Input::old('groups', array());
  92.  
  93. // Get all the available permissions
  94. $permissions = config('kit.permissions');
  95. $this->encodeAllPermissions($permissions);
  96.  
  97. // Selected permissions
  98. $selectedPermissions = Input::old('permissions', array('superuser' => -1));
  99. $this->encodePermissions($selectedPermissions);
  100.  
  101. // Show the page
  102. return View::make('kit::backend.users.create', compact('groups', 'selectedGroups', 'permissions', 'selectedPermissions'));
  103. }
  104.  
  105. /**
  106. * User create form processing.
  107. *
  108. * @return Redirect
  109. */
  110. public function postCreate()
  111. {
  112. if (! Sentinel::getUser()->hasAccess('user.create')) {
  113. // Redirect to the user management page
  114. return Redirect::route('users')->with('error', 'Insufficient permissions!');
  115. }
  116.  
  117. // Create a new validator instance from our validation rules
  118. $validator = Validator::make(Input::all(), $this->validationRules);
  119.  
  120. // If validation fails, we'll exit the operation now.
  121. if ($validator->fails()) {
  122. // Ooops.. something went wrong
  123. return Redirect::back()->withInput()->withErrors($validator);
  124. }
  125.  
  126. try {
  127. // We need to reverse the UI specific logic for our
  128. // permissions here before we create the user.
  129. $permissions = Input::get('permissions', array());
  130. $this->decodePermissions($permissions);
  131. app('request')->request->set('permissions', $permissions);
  132.  
  133. // Get the inputs, with some exceptions
  134. $inputs = Input::except('csrf_token', 'password_confirm', 'groups');
  135.  
  136. // Was the user created?
  137. if ($user = Sentinel::register($inputs, (bool) $inputs['activated'])) {
  138. // Assign the selected groups to this user
  139. foreach (Input::get('groups', array()) as $groupId) {
  140. $group = Sentinel::getRoleRepository()->findById($groupId);
  141.  
  142. $user->roles()->attach($group);
  143. }
  144.  
  145. // Prepare the success message
  146. $success = Lang::get('kit::admin/users/message.success.create');
  147.  
  148. // Redirect to the new user page
  149. return Redirect::route('update/user', $user->id)->with('success', $success);
  150. }
  151.  
  152. // Prepare the error message
  153. $error = Lang::get('kit::admin/users/message.error.create');
  154.  
  155. // Redirect to the user creation page
  156. return Redirect::route('create/user')->with('error', $error);
  157. } catch (LoginRequiredException $e) {
  158. $error = Lang::get('kit::admin/users/message.user_login_required');
  159. } catch (PasswordRequiredException $e) {
  160. $error = Lang::get('kit::admin/users/message.user_password_required');
  161. } catch (UserExistsException $e) {
  162. $error = Lang::get('kit::admin/users/message.user_exists');
  163. }
  164.  
  165. // Redirect to the user creation page
  166. return Redirect::route('create/user')->withInput()->with('error', $error);
  167. }
  168.  
  169. /**
  170. * User update.
  171. *
  172. * @param int $id
  173. * @return View
  174. */
  175. public function getEdit($id = null)
  176. {
  177. if (! Sentinel::getUser()->hasAccess('user.edit')) {
  178. // Redirect to the user management page
  179. return Redirect::route('users')->with('error', 'Insufficient permissions!');
  180. }
  181.  
  182. try {
  183. // Get the user information
  184. $user = Sentinel::getUserRepository()->findById($id);
  185.  
  186. // Get this user groups
  187. $userGroups = $user->roles()->lists('name', 'id')->toArray();
  188.  
  189. // Get this user permissions
  190. $userPermissions = array_merge(Input::old('permissions', array('superuser' => -1)), $user->getPermissions());
  191. $this->encodePermissions($userPermissions);
  192.  
  193. // Get a list of all the available groups
  194. $groups = Sentinel::getRoleRepository()->get();
  195.  
  196. // Get all the available permissions
  197. $permissions = config('kit.permissions');
  198. $this->encodeAllPermissions($permissions);
  199. } catch (UserNotFoundException $e) {
  200. // Prepare the error message
  201. $error = Lang::get('kit::admin/users/message.user_not_found', compact('id'));
  202.  
  203. // Redirect to the user management page
  204. return Redirect::route('users')->with('error', $error);
  205. }
  206.  
  207. // Show the page
  208. return View::make('kit::backend.users.edit', compact('user', 'groups', 'userGroups', 'permissions', 'userPermissions'));
  209. }
  210.  
  211. /**
  212. * User update form processing page.
  213. *
  214. * @param int $id
  215. * @return Redirect
  216. */
  217. public function postEdit($id = null)
  218. {
  219. if (! Sentinel::getUser()->hasAccess('user.edit')) {
  220. // Redirect to the user management page
  221. return Redirect::route('users')->with('error', 'Insufficient permissions!');
  222. }
  223.  
  224. try {
  225. // Get the user information
  226. $user = Sentinel::getUserRepository()->findById($id);
  227. } catch (UserNotFoundException $e) {
  228. // Prepare the error message
  229. $error = Lang::get('kit::admin/users/message.user_not_found', compact('id'));
  230.  
  231. // Redirect to the user management page
  232. return Redirect::route('users')->with('error', $error);
  233. }
  234.  
  235. //
  236. $this->validationRules['email'] = "required|email|unique:users,email,{$user->email},email";
  237.  
  238. // Do we want to update the user password?
  239. if (! $password = Input::get('password')) {
  240. unset($this->validationRules['password']);
  241. unset($this->validationRules['password_confirm']);
  242. #$this->validationRules['password'] = 'required|between:3,32';
  243. #$this->validationRules['password_confirm'] = 'required|between:3,32|same:password';
  244. }
  245.  
  246. // Create a new validator instance from our validation rules
  247. $validator = Validator::make(Input::all(), $this->validationRules);
  248.  
  249. // If validation fails, we'll exit the operation now.
  250. if ($validator->fails()) {
  251. // Ooops.. something went wrong
  252. return Redirect::back()->withInput()->withErrors($validator);
  253. }
  254.  
  255. try {
  256. // Update the user
  257. $user->first_name = Input::get('first_name');
  258. $user->last_name = Input::get('last_name');
  259. $user->email = Input::get('email');
  260.  
  261. $activation = $user->activations()->first();
  262. $activation->completed = Input::get('activated');
  263. $activation->save();
  264.  
  265. // Do we want to update the user password?
  266. if ($password) {
  267. $user->password = $password;
  268. }
  269.  
  270. // Get the current user groups
  271. $userGroups = $user->roles()->lists('role_id', 'role_id')->toArray();
  272.  
  273. // Get the selected groups
  274. $selectedGroups = Input::get('groups', array());
  275.  
  276. // Groups comparison between the groups the user currently
  277. // have and the groups the user wish to have.
  278. $groupsToAdd = array_diff($selectedGroups, $userGroups);
  279. $groupsToRemove = array_diff($userGroups, $selectedGroups);
  280.  
  281. // Assign the user to groups
  282. foreach ($groupsToAdd as $groupId) {
  283. $group = Sentinel::getRoleRepository()->findById($groupId);
  284.  
  285. $user->roles()->attach($group);
  286. }
  287.  
  288. // Remove the user from groups
  289. foreach ($groupsToRemove as $groupId) {
  290. $group = Sentinel::getRoleRepository()->findById($groupId);
  291.  
  292. $user->roles()->detach($group);
  293. }
  294.  
  295. // Was the user updated?
  296. if ($user->save()) {
  297. // Prepare the success message
  298. $success = Lang::get('kit::admin/users/message.success.update');
  299.  
  300. // Redirect to the user page
  301. return Redirect::route('update/user', $id)->with('success', $success);
  302. }
  303.  
  304. // Prepare the error message
  305. $error = Lang::get('kit::admin/users/message.error.update');
  306. } catch (LoginRequiredException $e) {
  307. $error = Lang::get('kit::admin/users/message.user_login_required');
  308. }
  309.  
  310. // Redirect to the user page
  311. return Redirect::route('update/user', $id)->withInput()->with('error', $error);
  312. }
  313.  
  314. /**
  315. * Delete the given user.
  316. *
  317. * @param int $id
  318. * @return Redirect
  319. */
  320. public function getDelete($id = null)
  321. {
  322. try {
  323. // Get user information
  324. $user = Sentinel::getUserRepository()->findById($id);
  325.  
  326. // Check if we are not trying to delete ourselves
  327. if ($user->id === Sentinel::getUser()->id) {
  328. // Prepare the error message
  329. $error = Lang::get('kit::admin/users/message.error.delete');
  330.  
  331. // Redirect to the user management page
  332. return Redirect::route('users')->with('error', $error);
  333. }
  334.  
  335. // Do we have permission to delete this user?
  336. if (! Sentinel::getUser()->hasAccess('user.delete')) {
  337. // Redirect to the user management page
  338. return Redirect::route('users')->with('error', 'Insufficient permissions!');
  339. }
  340.  
  341. // Delete the user
  342. $message = 'ban';
  343. if($user->isActivated()){
  344. $user->ban();
  345. }
  346. else {
  347. $user->activate();
  348. $message = 'activate';
  349. }
  350.  
  351. // Prepare the success message
  352. $success = Lang::get('kit::admin/users/message.success.'.$message);
  353.  
  354. // Redirect to the user management page
  355. return Redirect::back()->with('success', $success);
  356. } catch (UserNotFoundException $e) {
  357. // Prepare the error message
  358. $error = Lang::get('kit::admin/users/message.user_not_found', compact('id'));
  359.  
  360. // Redirect to the user management page
  361. return Redirect::back()->with('error', $error);
  362. }
  363. }
  364.  
  365. /**
  366. * Restore a deleted user.
  367. *
  368. * @param int $id
  369. * @return Redirect
  370. */
  371. public function getRestore($id = null)
  372. {
  373. try {
  374. // Get user information
  375. $user = Sentinel::getUserRepository()->createModel()->withTrashed()->find($id);
  376.  
  377. if (! Sentinel::getUser()->hasAccess('user.delete')) {
  378. // Redirect to the user management page
  379. return Redirect::route('users')->with('error', 'Insufficient permissions!');
  380. }
  381.  
  382. // Restore the user
  383. $user->restore();
  384.  
  385. // Prepare the success message
  386. $success = Lang::get('kit::admin/users/message.success.restored');
  387.  
  388. // Redirect to the user management page
  389. return Redirect::route('users')->with('success', $success);
  390. } catch (UserNotFoundException $e) {
  391. // Prepare the error message
  392. $error = Lang::get('kit::admin/users/message.user_not_found', compact('id'));
  393.  
  394. // Redirect to the user management page
  395. return Redirect::route('users')->with('error', $error);
  396. }
  397. }
  398. }
Add Comment
Please, Sign In to add comment