/** * Execute the console command. * * @return void */ public function fire() { try { $email = $this->argument('email'); $pass = $this->argument('password'); $username = $this->argument('username'); $groupName = $this->argument('group'); $validator = new UserValidator(array('email' => $email, 'pass' => $pass, 'username' => $username), 'create'); if (!$validator->passes()) { foreach ($validator->getErrors() as $key => $messages) { $this->info(ucfirst($key) . ' :'); foreach ($messages as $message) { $this->error($message); } } } else { // Create the user $user = \Sentry::getUserProvider()->create(array('email' => $email, 'password' => $pass, 'username' => $username)); $activationCode = $user->getActivationCode(); $user->attemptActivation($activationCode); if ($groupName !== NULL) { $group = \Sentry::getGroupProvider()->findByName($groupName); $user->addGroup($group); } $this->info('User created with success'); } } catch (\Cartalyst\Sentry\Users\UserExistsException $e) { $this->error('User already exists !'); } catch (\Cartalyst\Sentry\Groups\GroupNotFoundException $e) { $this->error('Group ' . $groupName . ' does not exists !'); } }
/** * Login post authentication */ public function postLogin() { try { $validator = new UserValidator(Input::all(), 'login'); $loginAttribute = Config::get('cartalyst/sentry::users.login_attribute'); if (!$validator->passes()) { return Response::json(array('logged' => false, 'errorMessages' => $validator->getErrors())); } $credentials = array($loginAttribute => Input::get($loginAttribute), 'password' => Input::get('pass')); // authenticate user Sentry::authenticate($credentials, (bool) Input::get('remember')); } catch (\Cartalyst\Sentry\Throttling\UserBannedException $e) { return Response::json(array('logged' => false, 'errorMessage' => trans('usermanager::all.messages.banned'), 'errorType' => 'danger')); } catch (\RuntimeException $e) { return Response::json(array('logged' => false, 'errorMessage' => trans('usermanager::all.messages.login-failed'), 'errorType' => 'danger')); } return Response::json(array('logged' => true)); }
/** * Update user account * @param int $userId * @return Response */ public function putShow($userId) { try { $validator = new UserValidator(Input::all(), 'update'); if (!$validator->passes()) { return Response::json(array('userUpdated' => false, 'errorMessages' => $validator->getErrors())); } $permissionsValues = Input::get('permission'); $permissions = $this->_formatPermissions($permissionsValues); // Find the user using the user id $user = Sentry::getUserProvider()->findById($userId); $user->username = Input::get('username'); $user->email = Input::get('email'); $user->last_name = Input::get('last_name'); $user->first_name = Input::get('first_name'); $user->permissions = $permissions; $currentUser = Sentry::getUser(); $permissions = empty($permissions) ? '' : json_encode($permissions); $hasPermissionManagement = $currentUser->hasAccess(Config::get('usermanager::permissions.addUserPermission')) || $currentUser->hasAccess('superuser'); if ($hasPermissionManagement === true) { DB::table('users')->where('id', $userId)->update(array('permissions' => $permissions)); } $pass = Input::get('pass'); if (!empty($pass)) { $user->password = $pass; } // Update the user if ($user->save()) { // if the user has permission to update $banned = Input::get('banned'); if (isset($banned) && Sentry::getUser()->getId() !== $user->getId()) { $this->_banUser($userId, $banned); } if ($currentUser->hasAccess(Config::get('usermanager::permissions.addUserGroup'))) { $groups = Input::get('groups') === null ? array() : Input::get('groups'); $userGroups = $user->getGroups()->toArray(); foreach ($userGroups as $group) { if (!in_array($group['id'], $groups)) { $group = Sentry::getGroupProvider()->findById($group['id']); $user->removeGroup($group); } } if (isset($groups) && is_array($groups)) { foreach ($groups as $groupId) { $group = Sentry::getGroupProvider()->findById($groupId); $user->addGroup($group); } } } return Response::json(array('userUpdated' => true, 'message' => trans('usermanager::users.messages.update-success'), 'messageType' => 'success')); } else { return Response::json(array('userUpdated' => false, 'message' => trans('usermanager::users.messages.update-fail'), 'messageType' => 'danger')); } } catch (\Cartalyst\Sentry\Users\UserExistsException $e) { return Response::json(array('userUpdated' => false, 'message' => trans('usermanager::users.messages.user-email-exists'), 'messageType' => 'danger')); } catch (\Exception $e) { return Response::json(array('userUpdated' => false, 'message' => trans('usermanager::users.messages.user-name-exists'), 'messageType' => 'danger')); } }