Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php namespace Kit\Http\Controllers\Admin;
- use Kit\Http\Controllers\AdminController;
- use Cartalyst\Sentinel\Users\LoginRequiredException;
- use Cartalyst\Sentinel\Users\PasswordRequiredException;
- use Cartalyst\Sentinel\Users\UserExistsException;
- use Cartalyst\Sentinel\Users\UserNotFoundException;
- use Config;
- use Input;
- use Lang;
- use Redirect;
- use Sentinel;
- use Validator;
- use View;
- use App\User;
- class UsersController extends AdminController
- {
- /**
- * Declare the rules for the form validation
- *
- * @var array
- */
- protected $validationRules = array(
- 'first_name' => 'required|min:3',
- 'last_name' => 'required|min:3',
- 'email' => 'required|email|unique:users,email',
- 'password' => 'required|between:3,32',
- 'password_confirm' => 'required|between:3,32|same:password',
- );
- /**
- * Show a list of all the users.
- *
- * @return View
- */
- public function getIndex()
- {
- // Grab all the users
- $users = Sentinel::getUserRepository()->createModel()->orderBy('users.created_at', 'DESC');
- if (Input::get('type')) {
- if (Input::get('type') == 'onlyTrainee') {
- // $users->has('traineeProfile');
- // $users->join('users','trainer_profiles.user_id','=','users.id','left outer');
- }
- elseif (Input::get('type') == 'onlyCorporateTrainee') {
- $users->join('corporate_profiles', 'users.id','=','corporate_profiles.trainee_profile_id');
- }
- elseif (Input::get('type') == 'onlyNotActivatedTrainers') {
- $users->join('activations','users.id','=','activations.user_id')
- ->where('completed', 0)
- ->orWhere('admin_approved', 0);
- }
- elseif (Input::get('type') == 'onlyTrainer') {
- $users->has('trainerProfile');
- }
- }
- // Paginate the users
- $users = $users->with('activations','trainerProfile','traineeProfile')->get();
- /*->appends(array(
- 'withTrashed' => Input::get('withTrashed'),
- 'onlyTrashed' => Input::get('onlyTrashed'),
- 'type' => Input::get('type'),
- ));*/
- // Show the page
- return View::make('kit::backend.users.index', compact('users'));
- }
- /**
- * User create.
- *
- * @return View
- */
- public function getCreate()
- {
- if (! Sentinel::getUser()->hasAccess('user.create')) {
- // Redirect to the user management page
- return Redirect::route('users')->with('error', 'Insufficient permissions!');
- }
- // Get all the available groups
- $groups = Sentinel::getRoleRepository()->get();
- // Selected groups
- $selectedGroups = Input::old('groups', array());
- // Get all the available permissions
- $permissions = config('kit.permissions');
- $this->encodeAllPermissions($permissions);
- // Selected permissions
- $selectedPermissions = Input::old('permissions', array('superuser' => -1));
- $this->encodePermissions($selectedPermissions);
- // Show the page
- return View::make('kit::backend.users.create', compact('groups', 'selectedGroups', 'permissions', 'selectedPermissions'));
- }
- /**
- * User create form processing.
- *
- * @return Redirect
- */
- public function postCreate()
- {
- if (! Sentinel::getUser()->hasAccess('user.create')) {
- // Redirect to the user management page
- return Redirect::route('users')->with('error', 'Insufficient permissions!');
- }
- // Create a new validator instance from our validation rules
- $validator = Validator::make(Input::all(), $this->validationRules);
- // If validation fails, we'll exit the operation now.
- if ($validator->fails()) {
- // Ooops.. something went wrong
- return Redirect::back()->withInput()->withErrors($validator);
- }
- try {
- // We need to reverse the UI specific logic for our
- // permissions here before we create the user.
- $permissions = Input::get('permissions', array());
- $this->decodePermissions($permissions);
- app('request')->request->set('permissions', $permissions);
- // Get the inputs, with some exceptions
- $inputs = Input::except('csrf_token', 'password_confirm', 'groups');
- // Was the user created?
- if ($user = Sentinel::register($inputs, (bool) $inputs['activated'])) {
- // Assign the selected groups to this user
- foreach (Input::get('groups', array()) as $groupId) {
- $group = Sentinel::getRoleRepository()->findById($groupId);
- $user->roles()->attach($group);
- }
- // Prepare the success message
- $success = Lang::get('kit::admin/users/message.success.create');
- // Redirect to the new user page
- return Redirect::route('update/user', $user->id)->with('success', $success);
- }
- // Prepare the error message
- $error = Lang::get('kit::admin/users/message.error.create');
- // Redirect to the user creation page
- return Redirect::route('create/user')->with('error', $error);
- } catch (LoginRequiredException $e) {
- $error = Lang::get('kit::admin/users/message.user_login_required');
- } catch (PasswordRequiredException $e) {
- $error = Lang::get('kit::admin/users/message.user_password_required');
- } catch (UserExistsException $e) {
- $error = Lang::get('kit::admin/users/message.user_exists');
- }
- // Redirect to the user creation page
- return Redirect::route('create/user')->withInput()->with('error', $error);
- }
- /**
- * User update.
- *
- * @param int $id
- * @return View
- */
- public function getEdit($id = null)
- {
- if (! Sentinel::getUser()->hasAccess('user.edit')) {
- // Redirect to the user management page
- return Redirect::route('users')->with('error', 'Insufficient permissions!');
- }
- try {
- // Get the user information
- $user = Sentinel::getUserRepository()->findById($id);
- // Get this user groups
- $userGroups = $user->roles()->lists('name', 'id')->toArray();
- // Get this user permissions
- $userPermissions = array_merge(Input::old('permissions', array('superuser' => -1)), $user->getPermissions());
- $this->encodePermissions($userPermissions);
- // Get a list of all the available groups
- $groups = Sentinel::getRoleRepository()->get();
- // Get all the available permissions
- $permissions = config('kit.permissions');
- $this->encodeAllPermissions($permissions);
- } catch (UserNotFoundException $e) {
- // Prepare the error message
- $error = Lang::get('kit::admin/users/message.user_not_found', compact('id'));
- // Redirect to the user management page
- return Redirect::route('users')->with('error', $error);
- }
- // Show the page
- return View::make('kit::backend.users.edit', compact('user', 'groups', 'userGroups', 'permissions', 'userPermissions'));
- }
- /**
- * User update form processing page.
- *
- * @param int $id
- * @return Redirect
- */
- public function postEdit($id = null)
- {
- if (! Sentinel::getUser()->hasAccess('user.edit')) {
- // Redirect to the user management page
- return Redirect::route('users')->with('error', 'Insufficient permissions!');
- }
- try {
- // Get the user information
- $user = Sentinel::getUserRepository()->findById($id);
- } catch (UserNotFoundException $e) {
- // Prepare the error message
- $error = Lang::get('kit::admin/users/message.user_not_found', compact('id'));
- // Redirect to the user management page
- return Redirect::route('users')->with('error', $error);
- }
- //
- $this->validationRules['email'] = "required|email|unique:users,email,{$user->email},email";
- // Do we want to update the user password?
- if (! $password = Input::get('password')) {
- unset($this->validationRules['password']);
- unset($this->validationRules['password_confirm']);
- #$this->validationRules['password'] = 'required|between:3,32';
- #$this->validationRules['password_confirm'] = 'required|between:3,32|same:password';
- }
- // Create a new validator instance from our validation rules
- $validator = Validator::make(Input::all(), $this->validationRules);
- // If validation fails, we'll exit the operation now.
- if ($validator->fails()) {
- // Ooops.. something went wrong
- return Redirect::back()->withInput()->withErrors($validator);
- }
- try {
- // Update the user
- $user->first_name = Input::get('first_name');
- $user->last_name = Input::get('last_name');
- $user->email = Input::get('email');
- $activation = $user->activations()->first();
- $activation->completed = Input::get('activated');
- $activation->save();
- // Do we want to update the user password?
- if ($password) {
- $user->password = $password;
- }
- // Get the current user groups
- $userGroups = $user->roles()->lists('role_id', 'role_id')->toArray();
- // Get the selected groups
- $selectedGroups = Input::get('groups', array());
- // Groups comparison between the groups the user currently
- // have and the groups the user wish to have.
- $groupsToAdd = array_diff($selectedGroups, $userGroups);
- $groupsToRemove = array_diff($userGroups, $selectedGroups);
- // Assign the user to groups
- foreach ($groupsToAdd as $groupId) {
- $group = Sentinel::getRoleRepository()->findById($groupId);
- $user->roles()->attach($group);
- }
- // Remove the user from groups
- foreach ($groupsToRemove as $groupId) {
- $group = Sentinel::getRoleRepository()->findById($groupId);
- $user->roles()->detach($group);
- }
- // Was the user updated?
- if ($user->save()) {
- // Prepare the success message
- $success = Lang::get('kit::admin/users/message.success.update');
- // Redirect to the user page
- return Redirect::route('update/user', $id)->with('success', $success);
- }
- // Prepare the error message
- $error = Lang::get('kit::admin/users/message.error.update');
- } catch (LoginRequiredException $e) {
- $error = Lang::get('kit::admin/users/message.user_login_required');
- }
- // Redirect to the user page
- return Redirect::route('update/user', $id)->withInput()->with('error', $error);
- }
- /**
- * Delete the given user.
- *
- * @param int $id
- * @return Redirect
- */
- public function getDelete($id = null)
- {
- try {
- // Get user information
- $user = Sentinel::getUserRepository()->findById($id);
- // Check if we are not trying to delete ourselves
- if ($user->id === Sentinel::getUser()->id) {
- // Prepare the error message
- $error = Lang::get('kit::admin/users/message.error.delete');
- // Redirect to the user management page
- return Redirect::route('users')->with('error', $error);
- }
- // Do we have permission to delete this user?
- if (! Sentinel::getUser()->hasAccess('user.delete')) {
- // Redirect to the user management page
- return Redirect::route('users')->with('error', 'Insufficient permissions!');
- }
- // Delete the user
- $message = 'ban';
- if($user->isActivated()){
- $user->ban();
- }
- else {
- $user->activate();
- $message = 'activate';
- }
- // Prepare the success message
- $success = Lang::get('kit::admin/users/message.success.'.$message);
- // Redirect to the user management page
- return Redirect::back()->with('success', $success);
- } catch (UserNotFoundException $e) {
- // Prepare the error message
- $error = Lang::get('kit::admin/users/message.user_not_found', compact('id'));
- // Redirect to the user management page
- return Redirect::back()->with('error', $error);
- }
- }
- /**
- * Restore a deleted user.
- *
- * @param int $id
- * @return Redirect
- */
- public function getRestore($id = null)
- {
- try {
- // Get user information
- $user = Sentinel::getUserRepository()->createModel()->withTrashed()->find($id);
- if (! Sentinel::getUser()->hasAccess('user.delete')) {
- // Redirect to the user management page
- return Redirect::route('users')->with('error', 'Insufficient permissions!');
- }
- // Restore the user
- $user->restore();
- // Prepare the success message
- $success = Lang::get('kit::admin/users/message.success.restored');
- // Redirect to the user management page
- return Redirect::route('users')->with('success', $success);
- } catch (UserNotFoundException $e) {
- // Prepare the error message
- $error = Lang::get('kit::admin/users/message.user_not_found', compact('id'));
- // Redirect to the user management page
- return Redirect::route('users')->with('error', $error);
- }
- }
- }
Add Comment
Please, Sign In to add comment