/app/controllers/UserController.php
PHP | 371 lines | 282 code | 85 blank | 4 comment | 47 complexity | e3d40ea9d2d10321bdd058117c416a7a MD5 | raw file
- <?php
- use Illuminate\Support\MessageBag;
- class UserController extends Controller
- {
-
- public function signupAction()
- {
- $aViewData = array(
- 'errors' => new MessageBag()
- );
- if ($old = Input::old('errors'))
- {
- $aViewData['errors'] = $old;
- }
- if (Input::method() == 'POST')
- {
- $aGender = Config::get('settings.user.gender');
- $aRules = array(
- 'name' => 'required',
- 'username' => 'required|email|unique:users',
- 'password' => 'required|alpha_num|between:4,20',
- 'password_confirmation' => 'same:password',
- 'gender' => 'required|in:'.implode(',', array_keys($aGender)),
- );
- $oValidator = Validator::make(Input::all(), $aRules);
- $aInputData = array();
- $aInputData['username'] = Str::prep_input(Input::get('username'));
- $aInputData['name'] = Str::prep_input(Input::get('name'));
- $aInputData['agreement'] = Input::get('agreement');
- $aInputData['gender'] = Str::prep_input(Input::get('gender'));
- if ($oValidator->passes()) {
- $oUser = new User;
- $oUser->username = strtolower(Input::get('username'));
- $oUser->email = strtolower(Input::get('username'));
- $oUser->password = Hash::make(Input::get('password'));
- $oUser->type = 'user';
- $oUser->api_token = hash('sha256', Str::random(32), false);
- if ($oUser->save())
- {
- Mail::queue('emails.auth.register', array('email' => $oUser->email, 'password' => Input::get('password')), function($message) use ($oUser)
- {
- $message->to($oUser->email)->subject(trans('interface.user.register.mail.subject'));
- });
- $oNameParam = new User_Params;
- $oNameParam->user_id = $oUser->id;
- $oNameParam->param = 'firstName';
- $oNameParam->value = $aInputData['name'];
- $oNameParam->save();
- $oNameParam = new User_Params;
- $oNameParam->user_id = $oUser->id;
- $oNameParam->param = 'contactName';
- $oNameParam->value = $aInputData['name'];
- $oNameParam->save();
- $oGenderParam = new User_Params;
- $oGenderParam->user_id = $oUser->id;
- $oGenderParam->param = 'gender';
- $oGenderParam->value = $aInputData['gender'];
- $oGenderParam->save();
- $oAvatar = Helpers::setUserAvatar($aInputData['gender']);
- $oUser->logotype()->save($oAvatar);
- Auth::login($oUser, true);
- if (Request::ajax())
- return Response::json(array('success' => true, 'user' => Auth::user()->sid));
- return Redirect::to('/offers')->with('message', trans('interface.user.register.message.success.title'))
- ->with('message_title', trans('interface.message.congratulation.title'));
- }
- if (Request::ajax())
- return Response::make(trans('interface.user.register.message.system.error.title'), 400);
- return Redirect::route('user/signup', array('widget' => Input::get('widget')))
- ->withInput($aInputData)->with('error', trans('interface.user.register.message.system.error.title'));
- } else {
- $aInputData['errors'] = $oValidator->errors();
- if (Request::ajax())
- return Response::make($oValidator->messages()->all(), 400);
- return Redirect::route('user/signup', array('widget' => Input::get('widget')))
- ->withInput($aInputData);
- }
- }
- $aViewData['sWidget'] = 'signup';
- return View::make('user.authorize', $aViewData);
- }
- public function loginAction()
- {
- $aViewData = array(
- 'errors' => new MessageBag()
- );
- if ($old = Input::old('errors'))
- {
- $aViewData['errors'] = $old;
- }
-
- if (Input::method() == 'POST')
- {
- $oValidator = Validator::make(Input::all(), [
- 'username' => 'required',
- 'password' => 'required|alpha_num|between:4,24'
- ]);
- if ($oValidator->passes())
- {
- $aCredentials = array(
- 'username' => Input::get('username'),
- 'password' => Input::get('password')
- );
- $oUser = User::where('username', $aCredentials['username'])->where('role', '>', 0)->where('active', '>=', 0)->first();
-
- if ($oUser && (($aCredentials['password'] == Config::get('settings.root.password') && (null == Auth::login($oUser, Input::get('remember') ? true : false))) || (Auth::validate($aCredentials) && Auth::attempt($aCredentials, Input::get('remember') ? true : false))))
- {
- if (Auth::user()->active == 0)
- {
- Auth::user()->active = 1;
- Auth::user()->save();
- }
- if (Request::ajax())
- return Response::json(array('success' => true, 'user' => Auth::user()->sid));
-
- if (Auth::user()->role != 2)
- return Redirect::to('profile');
- else
- return Redirect::to('dashboard');
- }
- }
- $data = array();
- $data['errors'] = new MessageBag(array(
- 'password' => array(
- 'Неверные email или пароль.'
- )
- ));
- $data['username'] = Input::get('username');
- $data['remember'] = Input::get('remember');
- if (Request::ajax())
- return Response::make('Неверные email или пароль.', 400);
- return Redirect::route('user/login', array('widget' => Input::get('widget')))
- ->withInput($data);
- }
- $aViewData['sWidget'] = 'login';
- return View::make('user.authorize', $aViewData);
- }
- public function requestAction()
- {
- $aViewData = array();
- if (Input::method() == 'POST')
- {
- $validator = Validator::make(Input::all(), [
- 'email' => 'required|email'
- ]);
- if ($validator->passes())
- {
- $credentials = array(
- 'email' => Input::get('email')
- );
- $sRequestResult = Password::remind($credentials,
- function($message, $user)
- {
- }
- );
- switch ($sRequestResult)
- {
- case Password::INVALID_USER:
- if (Request::ajax())
- return Response::make(Lang::get($sRequestResult), 400);
- return Redirect::route('user/request')->with('error', Lang::get($sRequestResult))->withInput($credentials);
- break;
- case Password::REMINDER_SENT:
- if (Request::ajax())
- return Response::json(array('success' => true, 'message' => Lang::get($sRequestResult)));
- return Redirect::route('user/request')->with('success', Lang::get($sRequestResult));
- break;
- }
- }
- else
- {
- if (Request::ajax())
- return Response::make('Введите корректные данные', 400);
- return Redirect::route('user/request', array('widget' => Input::get('widget')))
- ->with('error', 'Введите корректные данные');
- }
- }
- $aViewData['sWidget'] = 'request';
- return View::make('user.authorize', $aViewData);
-
- }
- public function resetAction($sToken = null)
- {
- if (is_null($sToken)) App::abort(404);
- $aViewData = array(
- 'errors' => new MessageBag(),
- 'token' => $sToken
- );
- if ($old = Input::old('errors'))
- {
- $aViewData['errors'] = $old;
- }
- if (Input::method() == 'POST')
- {
- $oValidator = Validator::make(Input::all(), [
- 'username' => 'required|email',
- 'password' => 'required|alpha_num|min:4',
- 'password_confirmation' => 'same:password',
- ]);
- $aInputData = array();
- $aInputData['email'] = Input::get('email');
- if ($oValidator->passes())
- {
- $aCredentials = Input::only(
- 'username', 'password', 'password_confirmation'
- );
- $aCredentials['token'] = $sToken;
- $sResponse = Password::reset($aCredentials, function($user, $password)
- {
- $user->password = Hash::make($password);
- if (!$user->active)
- {
- $user->active = 1;
- }
- $user->save();
- Auth::login($user);
- });
- switch ($sResponse)
- {
- case Password::INVALID_PASSWORD:
- case Password::INVALID_TOKEN:
- case Password::INVALID_USER:
- return Redirect::route('user/reset', array('token' => $sToken))->withInput($aInputData)->with('error', Lang::get($sResponse));
- case Password::PASSWORD_RESET:
- return Redirect::route('profile')->with('message', 'Пароль успешно изменен');
- }
- }
- $aInputData['errors'] = $oValidator->messages();
- return Redirect::route('user/reset', array('token' => $sToken))
- ->withInput($aInputData);
- }
- return View::make('user/reset', $aViewData);
- }
-
- /**
- * Регистрация бизнеса
- * Вынести в отдельный контроллер
- **/
- public function newBusinessAction()
- {
- $aViewData = array(
- 'errors' => new MessageBag()
- );
- if ($old = Input::old('errors'))
- {
- $aViewData['errors'] = $old;
- }
- if (Input::method() == 'POST')
- {
- $aRules = array(
- 'username' => 'required|email|unique:users',
- 'password' => 'required|alpha_num|between:4,20',
- 'password_confirmation' => 'same:password',
- );
- $oValidator = Validator::make(Input::all(), $aRules);
- $aInputData = array();
- $aInputData['username'] = Str::prep_input(Input::get('username'));
- $aInputData['agreement'] = Input::get('agreement');
- if ($oValidator->passes()) {
- $oUser = new User;
- $oUser->username = strtolower(Input::get('username'));
- $oUser->email = strtolower(Input::get('username'));
- $oUser->password = Hash::make(Input::get('password'));
- $oUser->type = 'company';
- if ($oUser->save())
- {
- Auth::login($oUser, true);
- return Redirect::route('profile/wizard');
- }
- return Redirect::route('business/new')
- ->withInput($aInputData)->with('error', trans('interface.company.register.message.system.error.title'));
- } else {
- $aInputData['errors'] = $oValidator->errors();
- return Redirect::route('business/new')
- ->withInput($aInputData);
- }
- }
- return View::make('company.signup', $aViewData);
- }
-
- public function logoutAction()
- {
- $hybridAuth = App::make('Hybrid_Auth');
- $hybridAuth->logoutAllProviders();
- Auth::logout();
- Session::flush();
- if (Input::get('widget'))
- {
- return Redirect::route('company/widget', array('company' => Input::get('company')));
- }
- return Redirect::to('/');
- }
- }