/** * Run the database seeds. * * @return void */ public function run() { Model::unguard(); // Create an admin user $user = Sentinel::create(['email' => '*****@*****.**', 'password' => 'test', 'first_name' => 'Nicolas', 'last_name' => 'Widart']); // Activate the admin directly $activation = Activation::create($user); Activation::complete($user, $activation->code); // Find the group using the group id $adminGroup = Sentinel::findRoleBySlug('admin'); // Assign the group to the user $adminGroup->users()->attach($user); }
/** * Store a newly created resource in storage. * * @return Response */ public function store(Request $request, UserCreateRequest $UserCreateRequest) { // $input = $request->all(); $slug = $request->input('roles'); $image = $request->file('image'); //find by slug the role $role = Sentinel::findRoleBySlug($slug); //create the user $time = Str::slug(\Carbon\Carbon::now()); if (isset($image)) { $ext = $image->getClientOriginalExtension(); $name = 'user_' . $time . '.' . $ext; $image->move(public_path('img/user_img'), $name); } $input = $request->except(['image']); $input['image'] = isset($image) ? $name : 'avatar-larus.jpeg'; $user = Sentinel::register($input); // we activate the user $activation = Activation::create($user); $code = $activation->code; //if the activation was success, we assignig the role to user if (Activation::complete($user, $code)) { $role->users()->attach($user); return \Redirect::to('users')->withSuccess('El usuario ha sido añadido.'); } }
private function tryActivate($email, $token) { $user = Sentinel::findByCredentials(['email' => $email]); if (Activation::complete($user, $token)) { return redirect()->route('auth.login'); } else { return redirect()->route('auth.login')->withInput()->withErrors(['token' => 'Token was incorrect']); } }
/** * Activate the given used id * @param int $userId * @param string $code * @return mixed */ public function activate($userId, $code) { $user = Sentinel::findById($userId); $success = Activation::complete($user, $code); if ($success) { event(new UserHasActivatedAccount($user)); } return $success; }
/** * Create a user and assign roles to it * @param array $data * @param array $roles * @param bool $activated */ public function createWithRoles($data, $roles, $activated = false) { $this->hashPassword($data); $user = $this->create((array) $data); if (!empty($roles)) { $user->roles()->attach($roles); } if ($activated) { $activation = Activation::create($user); Activation::complete($user, $activation->code); } }
/** * Handle posting of the form for the user registration. * * @return \Illuminate\Http\RedirectResponse */ public function processRegistration(Request $request) { $input = $request->all(); $rules = ['first_name' => 'required', 'last_name' => 'required', 'email' => 'required|email|unique:users', 'password' => 'required', 'retype_password' => 'required|same:password']; $validator = \Validator::make($input, $rules); if ($validator->fails()) { return \Redirect::back()->withInput()->withErrors($validator); } if ($user = Sentinel::register($input)) { $activation = Activation::create($user); $code = $activation->code; if (Activation::complete($user, $code)) { return \Redirect::to('login')->withSuccess('Your accout was successfully created. You might login now.')->with('userId', $user->getUserId()); } } return \Redirect::to('register')->withInput()->withErrors('Failed to register.'); }
/** * set activation complete * * @return boolean */ protected function activationComplete() { try { $activation = Activation::create($this->model); if (!Activation::complete($this->model, $activation->code)) { throw new ActivateException($this->model->id, $activation->code, 'fail'); } if ($this->callerActivationMethod(debug_backtrace())) { $this->model->is_active = true; $this->model->save(); } event(new $this->events['activationSuccess']($this->model)); return true; } catch (ActivateException $e) { $this->model->is_active = false; $this->model->save(); event(new $this->events['activationFail']($e->getId(), $e->getActivationCode(), $e->getType())); return false; } }
/** * get account activate metod * * @param integer $id * @param string $code * @return Redirector */ public function accountActivate($id, $code) { try { $user = Sentinel::findById($id); if (is_null($user) || !Activation::exists($user)) { throw new ActivateException($id, $code, 'not_found'); } if (!Activation::complete($user, $code)) { throw new ActivateException($id, $code, 'fail'); } $user->is_active = true; $user->save(); Sentinel::login($user); Flash::success(trans('laravel-user-module::auth.activation.success')); // event fire event(new ActivateSuccess($user)); return redirect(lmbRoute(config('laravel-user-module.url.redirect_route'))); } catch (ActivateException $e) { Flash::error(trans('laravel-user-module::auth.activation.' . $e->getType())); // event fire event(new ActivateFail($e->getId(), $e->getActivationCode(), $e->getType())); return redirect(lmbRoute('getLogin')); } }
/** * User update form processing page. * * @param int $id * @return Redirect */ public function postEdit($id = null) { try { // Get the user information $user = Sentinel::findById($id); } catch (UserNotFoundException $e) { // Prepare the error message $error = Lang::get('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']); } // 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'); $user->dob = Input::get('dob'); $user->bio = Input::get('bio'); $user->gender = Input::get('gender'); $user->country = Input::get('country'); $user->state = Input::get('state'); $user->city = Input::get('city'); $user->address = Input::get('address'); $user->postal = Input::get('postal'); // Do we want to update the user password? if ($password) { $user->password = Hash::make($password); } // is new image uploaded? if ($file = Input::file('pic')) { $fileName = $file->getClientOriginalName(); $extension = $file->getClientOriginalExtension() ?: 'png'; $folderName = '/uploads/users/'; $destinationPath = public_path() . $folderName; $safeName = str_random(10) . '.' . $extension; $file->move($destinationPath, $safeName); //delete old pic if exists if (File::exists(public_path() . $folderName . $user->pic)) { File::delete(public_path() . $folderName . $user->pic); } //save new file path into db $user->pic = $safeName; } // Get the current user groups $userRoles = $user->roles()->lists('id')->all(); // Get the selected groups $selectedRoles = Input::get('groups', array()); // Groups comparison between the groups the user currently // have and the groups the user wish to have. $rolesToAdd = array_diff($selectedRoles, $userRoles); $rolesToRemove = array_diff($userRoles, $selectedRoles); // Assign the user to groups foreach ($rolesToAdd as $roleId) { $role = Sentinel::findRoleById($roleId); $role->users()->attach($user); } // Remove the user from groups foreach ($rolesToRemove as $roleId) { $role = Sentinel::findRoleById($roleId); $role->users()->detach($user); } // Activate / De-activate user $status = $activation = Activation::completed($user); if (Input::get('activate') != $status) { if (Input::get('activate')) { $activation = Activation::exists($user); if ($activation) { Activation::complete($user, $activation->code); } } else { //remove existing activation record Activation::remove($user); //add new record Activation::create($user); //send activation mail $data = array('user' => $user, 'activationUrl' => URL::route('activate', $user->id, Activation::exists($user)->code)); // Send the activation code through email Mail::send('emails.register-activate', $data, function ($m) use($user) { $m->to($user->email, $user->first_name . ' ' . $user->last_name); $m->subject('Welcome ' . $user->first_name); }); } } // Was the user updated? if ($user->save()) { // Prepare the success message $success = Lang::get('users/message.success.update'); // Redirect to the user page return Redirect::route('users.update', $id)->with('success', $success); } // Prepare the error message $error = Lang::get('users/message.error.update'); } catch (LoginRequiredException $e) { $error = Lang::get('users/message.user_login_required'); } // Redirect to the user page return Redirect::route('users.update', $id)->withInput()->with('error', $error); }
public function postActivation(RegisterRequest $request) { $email = $request->input('email'); $user = Sentinel::findByCredentials(['email' => $email]); // 设置用户名和密码 Sentinel::update($user, array('first_name' => $request->input('username'), 'password' => $request->input('password'))); // 激活用户账号 $status = Activation::complete($user, $request->input('code')); if (!$status) { return redirect('auth/register')->withErrors(array('邮件链接过期,请重新注册')); } if (Session::has(self::OAUTH_USER)) { $oauthInfo = Session::get(self::OAUTH_USER); $provider = $oauthInfo['provider']; $oauthUser = $oauthInfo['user']; $uid = OAuthAccount::where('oauth_id', $oauthUser->getId())->where('oauth_type', $provider)->pluck('uid'); if (!$uid) { // 绑定账号 $oAuthAccount = new OAuthAccount(); $oAuthAccount->uid = $user->id; $oAuthAccount->oauth_id = $oauthUser->getId(); $oAuthAccount->oauth_type = $provider; $oAuthAccount->created_at = time(); $oAuthAccount->save(); } } // 登录 Sentinel::login($user); return redirect($this->redirectPath()); }
/** * User update form processing page. * * @param User $user * @param UserRequest $request * @return Redirect */ public function update(User $user, UserRequest $request) { try { $user->first_name = $request->get('first_name'); $user->last_name = $request->get('last_name'); $user->email = $request->get('email'); $user->dob = $request->get('dob'); $user->bio = $request->get('bio'); $user->gender = $request->get('gender'); $user->country = $request->get('country'); $user->state = $request->get('state'); $user->city = $request->get('city'); $user->address = $request->get('address'); $user->postal = $request->get('postal'); if ($password = $request->has('password')) { $user->password = Hash::make($password); } // is new image uploaded? if ($file = $request->file('pic')) { $extension = $file->getClientOriginalExtension() ?: 'png'; $folderName = '/uploads/users/'; $destinationPath = public_path() . $folderName; $safeName = str_random(10) . '.' . $extension; $file->move($destinationPath, $safeName); //delete old pic if exists if (File::exists(public_path() . $folderName . $user->pic)) { File::delete(public_path() . $folderName . $user->pic); } //save new file path into db $user->pic = $safeName; } //save record $user->save(); // Get the current user groups $userRoles = $user->roles()->lists('id')->all(); // Get the selected groups $selectedRoles = $request->get('groups', array()); // Groups comparison between the groups the user currently // have and the groups the user wish to have. $rolesToAdd = array_diff($selectedRoles, $userRoles); $rolesToRemove = array_diff($userRoles, $selectedRoles); // Assign the user to groups foreach ($rolesToAdd as $roleId) { $role = Sentinel::findRoleById($roleId); $role->users()->attach($user); } // Remove the user from groups foreach ($rolesToRemove as $roleId) { $role = Sentinel::findRoleById($roleId); $role->users()->detach($user); } // Activate / De-activate user $status = $activation = Activation::completed($user); if ($request->get('activate') != $status) { if ($request->get('activate')) { $activation = Activation::exists($user); if ($activation) { Activation::complete($user, $activation->code); } } else { //remove existing activation record Activation::remove($user); //add new record Activation::create($user); //send activation mail $data = array('user' => $user, 'activationUrl' => URL::route('activate', $user->id, Activation::exists($user)->code)); // Send the activation code through email Mail::send('emails.register-activate', $data, function ($m) use($user) { $m->to($user->email, $user->first_name . ' ' . $user->last_name); $m->subject('Welcome ' . $user->first_name); }); } } // Was the user updated? if ($user->save()) { // Prepare the success message $success = Lang::get('users/message.success.update'); // Redirect to the user page return Redirect::route('admin.users.edit', $user)->with('success', $success); } // Prepare the error message $error = Lang::get('users/message.error.update'); } catch (UserNotFoundException $e) { // Prepare the error message $error = Lang::get('users/message.user_not_found', compact('user')); // Redirect to the user management page return Redirect::route('users')->with('error', $error); } // Redirect to the user page return Redirect::route('admin.users.edit', $user)->withInput()->with('error', $error); }
/** * Update the specified resource in storage. * * @param int $id * @param UserRequest $request * @return Response */ public function update($id, UserRequest $request) { $user = Sentinel::findById($id); $credentials = $request->only(['first_name', 'last_name', 'email']); if ($request->has('password')) { $credentials['password'] = $request->get('password'); } $valid = Sentinel::getUserRepository()->validForUpdate($user, $credentials); if ($valid === false) { echo 'error'; return false; } Sentinel::getUserRepository()->update($user, $credentials); if ($request->get('active') != Activation::completed($user)) { if ($request->get('active')) { $activation = Activation::create($user); Activation::complete($user, $activation->code); } else { Activation::remove($user); } } $roles = Sentinel::getRoleRepository()->all(); $wantedRoles = $request->get('role'); foreach ($roles as $role) { if (in_array($role->id, $wantedRoles) && !$user->inRole($role)) { $role->users()->attach($user); } elseif (!in_array($role->id, $wantedRoles) && $user->inRole($role)) { $role->users()->detach($user); } } return redirect()->route('admin.users.show', [$user->id]); }
/** * Activate the given used id * @param int $userId * @param string $code * @return mixed */ public function activate($userId, $code) { $user = Sentinel::findById($userId); return Activation::complete($user, $code); }
/** * User account activation page. * * @param number $userId * @param string $activationCode * */ public function getActivate($userId, $activationCode) { // Is the user logged in? if (Sentinel::check()) { return Redirect::route('dashboard'); } $user = Sentinel::findById($userId); if (Activation::complete($user, $activationCode)) { // Activation was successfull return Redirect::route('admin-login')->with('success', Lang::get('auth/message.activate.success')); } else { // Activation not found or not completed. $error = Lang::get('auth/message.activate.error'); return Redirect::route('admin-login')->with('error', $error); } }
/** * 激活已注册用户 - 业务逻辑 * * @param $user_id * @param string $route_success * @param string $route_failed * @return \Illuminate\Http\RedirectResponse */ public function active_logic($user_id, $route_success = 'account_list_get', $route_failed = 'account_list_get') { $user_info = Sentinel::findById($user_id); if (!$user_info) { // dump('用户不存在'); return redirect()->route($route_failed); } //TODO::检查是否已完成激活 $activation_completed_return = Activation::completed($user_info); if ($activation_completed_return) { // dump('账号已激活'); return redirect()->route($route_failed); } else { //TODO::检查是否已生成激活码 $activation_code = ''; $activation_exists_return = Activation::exists($user_info); if ($activation_exists_return) { // dump('已生成激活码'); $activation_code = $activation_exists_return['code']; } else { // dump('第一次生成激活码'); //TODO::生成激活码,返回生成的activations表记录 $activation_create_return = Activation::create($user_info); $activation_code = $activation_create_return['code']; } // dump($activation_code); //TODO::使用激活码,激活用户 $activation_complete_return = Activation::complete($user_info, $activation_code); if ($activation_complete_return) { // dump('完成激活'); return redirect()->route($route_success); } else { // dump('激活失败'); return redirect()->route($route_failed); } } }
private function activate($user) { $activation = Activation::create($user); Activation::complete($user, $activation->code); }
/** * {@inheritDoc} */ public function activate(array $data) { // Setup validation rules. $this->rules = ['email' => 'required|email', 'activation_code' => 'required']; // Run validation. $this->validate($data); $user = $this->findByCredentials(['login' => $data['email']]); if (!Activation::complete($user, $data['activation_code'])) { throw new AuthenticationException(trans('dashboard::dashboard.errors.auth.activation.complete')); } return true; }
/** * Handle the activation request to the application. * * @Get("auth/activate/{user_id}/{activation_code}") * * @param $user_id * @param $activation_code * @return Redirect * @author Bertrand Kintanar */ public function getActivateUser($user_id, $activation_code) { $auth = $this->auth; $user = $auth::findById($user_id); // User not found if (!$user) { App::abort(404, 'user_not_found'); } if (Activation::complete($user, $activation_code)) { $auth::login($user); return redirect('/'); } else { App::abort(404, 'activation_code_invalid'); } }
/** * Check and manually activate or remove activation for the user * @param $user * @param array $data */ private function checkForManualActivation($user, array &$data) { if (Activation::completed($user) && !$data['activated']) { return Activation::remove($user); } if (!Activation::completed($user) && $data['activated']) { $activation = Activation::create($user); return Activation::complete($user, $activation->code); } }
public function getActivation($id = null, $code = null) { if (is_null($id) || is_null($code)) { throw new NotFoundHttpException(); } $user = Sentinel::findById($id); if (!empty($user) || $user != null) { if (Activation::exists($user) != false) { if (Activation::completed($user) != false) { list($type, $msg) = ['error', 'auth.validatecompleted']; } else { Activation::complete($user, $code); list($type, $msg) = ['mensaje', 'auth.validate_email']; } } else { list($type, $msg) = ['error', 'auth.validatenotfound']; } } else { list($type, $msg) = ['error', 'auth.usernotfound']; } return redirect('/')->with($type, trans($msg)); }