PageRenderTime 68ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Http/Controllers/Admin/AdminController.php

https://gitlab.com/sawmainek/528Express-Server
PHP | 423 lines | 321 code | 60 blank | 42 comment | 56 complexity | 6e118d760acdb3a571e8eed55eb7c6b8 MD5 | raw file
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use Illuminate\Http\Request;
  4. use Sentinel;
  5. use Activation;
  6. use DB;
  7. use URL;
  8. use Reminder;
  9. use App\Role;
  10. use Config;
  11. use Mail;
  12. use App\Delivery_staff;
  13. use Session;
  14. use App\User;
  15. use App\Shipper;
  16. use App\Http\Requests;
  17. use App\Http\Controllers\Controller;
  18. use Validator,ErrorException;
  19. use \Cartalyst\Sentinel\Checkpoints\NotActivatedException;
  20. use \Cartalyst\Sentinel\Checkpoints\ThrottlingException;
  21. class AdminController extends Controller
  22. {
  23. /**
  24. * Account sign in.
  25. *
  26. * @return View
  27. */
  28. public function getLogin()
  29. {
  30. // Is the user logged in?
  31. if (Sentinel::check()) {
  32. return redirect('admin/dashboard');
  33. }
  34. // Show the page
  35. return View('admin.login');
  36. }
  37. /**
  38. * Account sign in form processing.
  39. *
  40. * @return Redirect
  41. */
  42. public function postLogin(Request $request)
  43. {
  44. /*Check validation*/
  45. $validator = Validator::make($request->all(), [
  46. 'email' => 'required',
  47. 'password' => 'required|min:6',
  48. ]);
  49. // If validation fails, we'll exit the operation now.
  50. if ($validator->fails()) {
  51. if ($request->route()->getPrefix() == "/admin") {
  52. return redirect()->back()
  53. ->withErrors($validator)
  54. ->withInput();
  55. }
  56. if($validator->errors()->has('email'))
  57. return response()->json($validator->errors()->first('email'), 400);
  58. if($validator->errors()->has('password'))
  59. return response()->json($validator->errors()->first('password'), 400);
  60. }
  61. $credentials = [
  62. 'login' => $request->email,
  63. 'password' => $request->password,
  64. ];
  65. try {
  66. // Try to log the user in
  67. if($user = Sentinel::authenticate($credentials))
  68. {
  69. $user = User::with('roles')->whereid($user->id)->first();
  70. $staff = Delivery_staff::where('user_id',$user->id)->first();
  71. $user['staff']=$staff;
  72. return response()->json($user);
  73. }
  74. $message = 'Invalid Username or Password';
  75. } catch (NotActivatedException $e) {
  76. $credentials = [
  77. 'login' => $request->email,
  78. ];
  79. $user = Sentinel::findByCredentials($credentials);
  80. return response()->json($user,403);
  81. } catch (ThrottlingException $e) {
  82. $delay = $e->getDelay();
  83. $message = "Too many login attemps.Please try again in {$delay} second(s).";
  84. }
  85. // Redirect back to login page if prefix is admin
  86. if ($request->route()->getPrefix() == "/admin")
  87. {
  88. return redirect()->back()->withErrors(array('message'=>$message));
  89. }
  90. // Response json if prefix is api
  91. return response()->json($message,400);
  92. }
  93. public function postSignin(Request $request)
  94. {
  95. /*Check validation*/
  96. $validator = Validator::make($request->all(), [
  97. 'email' => 'required|email',
  98. 'password' => 'required|min:6',
  99. ]);
  100. // If validation fails, we'll exit the operation now.
  101. if ($validator->fails()) {
  102. if ($request->route()->getPrefix() == "/admin") {
  103. return redirect()->back()
  104. ->withErrors($validator)
  105. ->withInput();
  106. }
  107. if($validator->errors()->has('email'))
  108. return response()->json($validator->errors()->first('email'), 400);
  109. if($validator->errors()->has('password'))
  110. return response()->json($validator->errors()->first('password'), 400);
  111. }
  112. $credentials = [
  113. 'email' => $request->email,
  114. 'password' => $request->password,
  115. ];
  116. try {
  117. $user = Sentinel::findByCredentials($credentials);
  118. if ($user == null) {
  119. $message = 'Invalid Username or Password';
  120. if ($request->route()->getPrefix() == "/admin")
  121. {
  122. return redirect()->back()->withErrors(array('message'=>$message));
  123. }
  124. return response()->json($message);
  125. }
  126. if ($user->inRole("shipper")) {
  127. $message = "You are not allow for Admin Panel!";
  128. if ($request->route()->getPrefix() == "/admin")
  129. {
  130. return redirect()->back()->withErrors(array('message'=>$message));
  131. }
  132. return response()->json($message);
  133. }
  134. // Try to log the user in
  135. if(Sentinel::authenticate($credentials))
  136. {
  137. // Redirect to the dashboard page if prefix is admin
  138. if ($request->route()->getPrefix() == "/admin")
  139. {
  140. return redirect('admin/dashboard');
  141. }
  142. // Response json if prefix is api
  143. return response()->json($user);
  144. }
  145. $message = 'Invalid Username or Password';
  146. } catch (NotActivatedException $e) {
  147. $message = "Your account is not activate!";
  148. } catch (ThrottlingException $e) {
  149. $delay = $e->getDelay();
  150. $message = "Too many login attemps.Please try again in {$delay} second(s).";
  151. }
  152. // Redirect back to login page if prefix is admin
  153. if ($request->route()->getPrefix() == "/admin")
  154. {
  155. return redirect()->back()->withErrors(array('message'=>$message));
  156. }
  157. }
  158. /**
  159. * Account register
  160. *
  161. * @return View
  162. */
  163. public function getRegister()
  164. {
  165. return view('admin.register');
  166. }
  167. /**
  168. * Account register form processing.
  169. *
  170. * @return Redirect
  171. */
  172. public function postRegister(Request $request)
  173. {
  174. $validator = Validator::make($request->all(), [
  175. 'name' => 'required|unique:users,name',
  176. 'email' => 'required|email|max:255|unique:users,email',
  177. 'password' => 'required|min:6',
  178. 'phone' => 'required|numeric|min:8|unique:users,phone',
  179. ]);
  180. if ($validator->fails()) {
  181. if($validator->errors()->has('name'))
  182. return response()->json($validator->errors()->first('name'), 400);
  183. if($validator->errors()->has('email'))
  184. return response()->json($validator->errors()->first('email'), 400);
  185. if($validator->errors()->has('password'))
  186. return response()->json($validator->errors()->first('password'), 400);
  187. if($validator->errors()->has('phone'))
  188. return response()->json($validator->errors()->first('phone'), 400);
  189. }
  190. if ($file = $request->file('pic'))
  191. {
  192. $fileName = $file->getClientOriginalName();
  193. $extension = $file->getClientOriginalExtension() ?: 'png';
  194. $folderName = '/uploads/users/';
  195. $destinationPath = public_path() . $folderName;
  196. $safeName = str_random(10).'.'.$extension;
  197. $file->move($destinationPath, $safeName);
  198. }
  199. $credentials = [
  200. 'name' => $request->name,
  201. 'email' => $request->email,
  202. 'password' => $request->password,
  203. 'phone' => $request->phone,
  204. 'code' => mt_rand(100000, 999999),
  205. 'photo' => isset($safeName)?$safeName:$request->photo,
  206. ];
  207. DB::beginTransaction();
  208. try
  209. {
  210. $user = Sentinel::register($credentials);
  211. $data = array(
  212. 'user' => $user,
  213. );
  214. Mail::send('admin.emails.confirmcode', $data, function ($m) use ($user) {
  215. $m->from(Config::get('mail.from.address'),Config::get('mail.from.name'));
  216. $m->to($user->email, $user->name);
  217. $m->subject('Your 528express Verification code');
  218. });
  219. $curl = curl_init("http://shopyface.com/api/v1/sms");
  220. curl_setopt( $curl, CURLOPT_POST , true);
  221. curl_setopt( $curl, CURLOPT_POSTFIELDS, array(
  222. 'mobiles' => $user->phone,
  223. 'message' => "Your 528express Verification code is ".$user->code,
  224. ));
  225. curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1);
  226. $auth = curl_exec( $curl );
  227. $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  228. curl_close($curl);
  229. if($http_status == 200){
  230. $role = $request->role ? $request->role : "shipper";
  231. if($role == "shipper")
  232. {
  233. $rolename = Sentinel::findRoleByName($role);
  234. if($rolename)
  235. $rolename->users()->attach($user);
  236. $shipper = new Shipper;
  237. $shipper->user_id = $user->id;
  238. $shipper->name = $request->name;
  239. $shipper->save();
  240. }
  241. //Sentinel::login($user, false);
  242. }
  243. else{
  244. return response()->json('Sorry Something wrong!',400);
  245. }
  246. }
  247. catch (Exception $e)
  248. {
  249. DB::rollBack();
  250. }
  251. DB::commit();
  252. if ($request->route()->getPrefix() == "/admin") {
  253. return redirect('admin/dashboard');
  254. }
  255. return response()->json($user);
  256. }
  257. public function getActivate($userId,$activationCode = null)
  258. {
  259. if ($activationCode == 0) {
  260. return response()->json("Sorry Your code is not match!",400);
  261. }
  262. else{
  263. $user = Sentinel::findById($userId);
  264. $activation = Activation::where('user_id',$user->id)->first();
  265. if ($activation == null) {
  266. if ($user->code == $activationCode) {
  267. $activation = Activation::create($user);
  268. if (Activation::complete($user, $activation->code))
  269. {
  270. return response()->json($user);
  271. }
  272. else
  273. {
  274. return response()->json("Activation not found or not completed",400);
  275. }
  276. }
  277. else{
  278. return response()->json("Sorry Your code is not match!",400);
  279. }
  280. }
  281. else{
  282. return response()->json("Your acoount is already Activated!",400);
  283. }
  284. }
  285. }
  286. public function getResend($phone)
  287. {
  288. $user = User::where('phone',$phone)->first();
  289. $activation = Activation::where('user_id',$user->id)->first();
  290. if ($activation == null) {
  291. $curl = curl_init("http://shopyface.com/api/v1/sms");
  292. curl_setopt( $curl, CURLOPT_POST , true);
  293. curl_setopt( $curl, CURLOPT_POSTFIELDS, array(
  294. 'mobiles' => $user->phone,
  295. 'message' => "Your Varification code is ".$user->code,
  296. ));
  297. curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1);
  298. $auth = curl_exec( $curl );
  299. $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  300. curl_close($curl);
  301. if($http_status == 200){
  302. return response()->json('Resend your code. Please check your mobile phone!');
  303. }
  304. else{
  305. return response()->json('Sorry Something wrong!',400);
  306. }
  307. }
  308. else{
  309. return response()->json("Your account is already Activated!!");
  310. }
  311. }
  312. public function getForgotpassword()
  313. {
  314. return View('admin.forgot-password');
  315. }
  316. public function postForgotpassword(Request $request)
  317. {
  318. $validator = Validator::make($request->all(), [
  319. 'email' => 'required|email|exists:users,email',
  320. ]);
  321. // If validation fails, we'll exit the operation now.
  322. if ($validator->fails()) {
  323. if ($request->route()->getPrefix() == "/admin") {
  324. return redirect()->back()
  325. ->withErrors($validator)
  326. ->withInput();
  327. }
  328. if($validator->errors()->has('email'))
  329. return response()->json($validator->errors()->first('email'), 400);
  330. }
  331. $credentials = [
  332. 'email' => $request->email,
  333. ];
  334. $user = Sentinel::findByCredentials($credentials);
  335. if($user)
  336. {
  337. //get reminder for user
  338. $reminder = Reminder::exists($user) ?: Reminder::create($user);
  339. // Data to be used on the email view
  340. $data = array(
  341. 'user' => $user,
  342. 'forgotPasswordUrl' => URL::route('forgot-password-confirm',[$user->id, $reminder->code]),
  343. );
  344. // Send the activation code through email
  345. Mail::send('admin.emails.forgot-password', $data, function ($m) use ($user) {
  346. $m->to($user->email, $user->first_name . ' ' . $user->last_name);
  347. $m->subject('Account Password Recovery');
  348. });
  349. }
  350. else
  351. {
  352. // Even though the email was not found, we will pretend
  353. // we have sent the password reset code through email,
  354. // this is a security measure against hackers.
  355. }
  356. }
  357. public function getLogout()
  358. {
  359. // Log the user out
  360. Sentinel::logout();
  361. return redirect('admin/login');
  362. }
  363. }