PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/UserController.php

https://gitlab.com/melentev-av/perki.dev
PHP | 371 lines | 282 code | 85 blank | 4 comment | 47 complexity | e3d40ea9d2d10321bdd058117c416a7a MD5 | raw file
  1. <?php
  2. use Illuminate\Support\MessageBag;
  3. class UserController extends Controller
  4. {
  5. public function signupAction()
  6. {
  7. $aViewData = array(
  8. 'errors' => new MessageBag()
  9. );
  10. if ($old = Input::old('errors'))
  11. {
  12. $aViewData['errors'] = $old;
  13. }
  14. if (Input::method() == 'POST')
  15. {
  16. $aGender = Config::get('settings.user.gender');
  17. $aRules = array(
  18. 'name' => 'required',
  19. 'username' => 'required|email|unique:users',
  20. 'password' => 'required|alpha_num|between:4,20',
  21. 'password_confirmation' => 'same:password',
  22. 'gender' => 'required|in:'.implode(',', array_keys($aGender)),
  23. );
  24. $oValidator = Validator::make(Input::all(), $aRules);
  25. $aInputData = array();
  26. $aInputData['username'] = Str::prep_input(Input::get('username'));
  27. $aInputData['name'] = Str::prep_input(Input::get('name'));
  28. $aInputData['agreement'] = Input::get('agreement');
  29. $aInputData['gender'] = Str::prep_input(Input::get('gender'));
  30. if ($oValidator->passes()) {
  31. $oUser = new User;
  32. $oUser->username = strtolower(Input::get('username'));
  33. $oUser->email = strtolower(Input::get('username'));
  34. $oUser->password = Hash::make(Input::get('password'));
  35. $oUser->type = 'user';
  36. $oUser->api_token = hash('sha256', Str::random(32), false);
  37. if ($oUser->save())
  38. {
  39. Mail::queue('emails.auth.register', array('email' => $oUser->email, 'password' => Input::get('password')), function($message) use ($oUser)
  40. {
  41. $message->to($oUser->email)->subject(trans('interface.user.register.mail.subject'));
  42. });
  43. $oNameParam = new User_Params;
  44. $oNameParam->user_id = $oUser->id;
  45. $oNameParam->param = 'firstName';
  46. $oNameParam->value = $aInputData['name'];
  47. $oNameParam->save();
  48. $oNameParam = new User_Params;
  49. $oNameParam->user_id = $oUser->id;
  50. $oNameParam->param = 'contactName';
  51. $oNameParam->value = $aInputData['name'];
  52. $oNameParam->save();
  53. $oGenderParam = new User_Params;
  54. $oGenderParam->user_id = $oUser->id;
  55. $oGenderParam->param = 'gender';
  56. $oGenderParam->value = $aInputData['gender'];
  57. $oGenderParam->save();
  58. $oAvatar = Helpers::setUserAvatar($aInputData['gender']);
  59. $oUser->logotype()->save($oAvatar);
  60. Auth::login($oUser, true);
  61. if (Request::ajax())
  62. return Response::json(array('success' => true, 'user' => Auth::user()->sid));
  63. return Redirect::to('/offers')->with('message', trans('interface.user.register.message.success.title'))
  64. ->with('message_title', trans('interface.message.congratulation.title'));
  65. }
  66. if (Request::ajax())
  67. return Response::make(trans('interface.user.register.message.system.error.title'), 400);
  68. return Redirect::route('user/signup', array('widget' => Input::get('widget')))
  69. ->withInput($aInputData)->with('error', trans('interface.user.register.message.system.error.title'));
  70. } else {
  71. $aInputData['errors'] = $oValidator->errors();
  72. if (Request::ajax())
  73. return Response::make($oValidator->messages()->all(), 400);
  74. return Redirect::route('user/signup', array('widget' => Input::get('widget')))
  75. ->withInput($aInputData);
  76. }
  77. }
  78. $aViewData['sWidget'] = 'signup';
  79. return View::make('user.authorize', $aViewData);
  80. }
  81. public function loginAction()
  82. {
  83. $aViewData = array(
  84. 'errors' => new MessageBag()
  85. );
  86. if ($old = Input::old('errors'))
  87. {
  88. $aViewData['errors'] = $old;
  89. }
  90. if (Input::method() == 'POST')
  91. {
  92. $oValidator = Validator::make(Input::all(), [
  93. 'username' => 'required',
  94. 'password' => 'required|alpha_num|between:4,24'
  95. ]);
  96. if ($oValidator->passes())
  97. {
  98. $aCredentials = array(
  99. 'username' => Input::get('username'),
  100. 'password' => Input::get('password')
  101. );
  102. $oUser = User::where('username', $aCredentials['username'])->where('role', '>', 0)->where('active', '>=', 0)->first();
  103. 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))))
  104. {
  105. if (Auth::user()->active == 0)
  106. {
  107. Auth::user()->active = 1;
  108. Auth::user()->save();
  109. }
  110. if (Request::ajax())
  111. return Response::json(array('success' => true, 'user' => Auth::user()->sid));
  112. if (Auth::user()->role != 2)
  113. return Redirect::to('profile');
  114. else
  115. return Redirect::to('dashboard');
  116. }
  117. }
  118. $data = array();
  119. $data['errors'] = new MessageBag(array(
  120. 'password' => array(
  121. 'Неверные email или пароль.'
  122. )
  123. ));
  124. $data['username'] = Input::get('username');
  125. $data['remember'] = Input::get('remember');
  126. if (Request::ajax())
  127. return Response::make('Неверные email или пароль.', 400);
  128. return Redirect::route('user/login', array('widget' => Input::get('widget')))
  129. ->withInput($data);
  130. }
  131. $aViewData['sWidget'] = 'login';
  132. return View::make('user.authorize', $aViewData);
  133. }
  134. public function requestAction()
  135. {
  136. $aViewData = array();
  137. if (Input::method() == 'POST')
  138. {
  139. $validator = Validator::make(Input::all(), [
  140. 'email' => 'required|email'
  141. ]);
  142. if ($validator->passes())
  143. {
  144. $credentials = array(
  145. 'email' => Input::get('email')
  146. );
  147. $sRequestResult = Password::remind($credentials,
  148. function($message, $user)
  149. {
  150. }
  151. );
  152. switch ($sRequestResult)
  153. {
  154. case Password::INVALID_USER:
  155. if (Request::ajax())
  156. return Response::make(Lang::get($sRequestResult), 400);
  157. return Redirect::route('user/request')->with('error', Lang::get($sRequestResult))->withInput($credentials);
  158. break;
  159. case Password::REMINDER_SENT:
  160. if (Request::ajax())
  161. return Response::json(array('success' => true, 'message' => Lang::get($sRequestResult)));
  162. return Redirect::route('user/request')->with('success', Lang::get($sRequestResult));
  163. break;
  164. }
  165. }
  166. else
  167. {
  168. if (Request::ajax())
  169. return Response::make('Введите корректные данные', 400);
  170. return Redirect::route('user/request', array('widget' => Input::get('widget')))
  171. ->with('error', 'Введите корректные данные');
  172. }
  173. }
  174. $aViewData['sWidget'] = 'request';
  175. return View::make('user.authorize', $aViewData);
  176. }
  177. public function resetAction($sToken = null)
  178. {
  179. if (is_null($sToken)) App::abort(404);
  180. $aViewData = array(
  181. 'errors' => new MessageBag(),
  182. 'token' => $sToken
  183. );
  184. if ($old = Input::old('errors'))
  185. {
  186. $aViewData['errors'] = $old;
  187. }
  188. if (Input::method() == 'POST')
  189. {
  190. $oValidator = Validator::make(Input::all(), [
  191. 'username' => 'required|email',
  192. 'password' => 'required|alpha_num|min:4',
  193. 'password_confirmation' => 'same:password',
  194. ]);
  195. $aInputData = array();
  196. $aInputData['email'] = Input::get('email');
  197. if ($oValidator->passes())
  198. {
  199. $aCredentials = Input::only(
  200. 'username', 'password', 'password_confirmation'
  201. );
  202. $aCredentials['token'] = $sToken;
  203. $sResponse = Password::reset($aCredentials, function($user, $password)
  204. {
  205. $user->password = Hash::make($password);
  206. if (!$user->active)
  207. {
  208. $user->active = 1;
  209. }
  210. $user->save();
  211. Auth::login($user);
  212. });
  213. switch ($sResponse)
  214. {
  215. case Password::INVALID_PASSWORD:
  216. case Password::INVALID_TOKEN:
  217. case Password::INVALID_USER:
  218. return Redirect::route('user/reset', array('token' => $sToken))->withInput($aInputData)->with('error', Lang::get($sResponse));
  219. case Password::PASSWORD_RESET:
  220. return Redirect::route('profile')->with('message', 'Пароль успешно изменен');
  221. }
  222. }
  223. $aInputData['errors'] = $oValidator->messages();
  224. return Redirect::route('user/reset', array('token' => $sToken))
  225. ->withInput($aInputData);
  226. }
  227. return View::make('user/reset', $aViewData);
  228. }
  229. /**
  230. * Регистрация бизнеса
  231. * Вынести в отдельный контроллер
  232. **/
  233. public function newBusinessAction()
  234. {
  235. $aViewData = array(
  236. 'errors' => new MessageBag()
  237. );
  238. if ($old = Input::old('errors'))
  239. {
  240. $aViewData['errors'] = $old;
  241. }
  242. if (Input::method() == 'POST')
  243. {
  244. $aRules = array(
  245. 'username' => 'required|email|unique:users',
  246. 'password' => 'required|alpha_num|between:4,20',
  247. 'password_confirmation' => 'same:password',
  248. );
  249. $oValidator = Validator::make(Input::all(), $aRules);
  250. $aInputData = array();
  251. $aInputData['username'] = Str::prep_input(Input::get('username'));
  252. $aInputData['agreement'] = Input::get('agreement');
  253. if ($oValidator->passes()) {
  254. $oUser = new User;
  255. $oUser->username = strtolower(Input::get('username'));
  256. $oUser->email = strtolower(Input::get('username'));
  257. $oUser->password = Hash::make(Input::get('password'));
  258. $oUser->type = 'company';
  259. if ($oUser->save())
  260. {
  261. Auth::login($oUser, true);
  262. return Redirect::route('profile/wizard');
  263. }
  264. return Redirect::route('business/new')
  265. ->withInput($aInputData)->with('error', trans('interface.company.register.message.system.error.title'));
  266. } else {
  267. $aInputData['errors'] = $oValidator->errors();
  268. return Redirect::route('business/new')
  269. ->withInput($aInputData);
  270. }
  271. }
  272. return View::make('company.signup', $aViewData);
  273. }
  274. public function logoutAction()
  275. {
  276. $hybridAuth = App::make('Hybrid_Auth');
  277. $hybridAuth->logoutAllProviders();
  278. Auth::logout();
  279. Session::flush();
  280. if (Input::get('widget'))
  281. {
  282. return Redirect::route('company/widget', array('company' => Input::get('company')));
  283. }
  284. return Redirect::to('/');
  285. }
  286. }