Advertisement
Guest User

Untitled

a guest
Apr 4th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6.  
  7. use DateTime;
  8.  
  9. use Mail;
  10. use Hash;
  11. use Auth;
  12. use Session;
  13. use DB;
  14.  
  15. use App\Category;
  16. use App\SubCategory;
  17. use App\Config;
  18. use App\User;
  19. use App\Http\Requests;
  20. use App\Http\Controllers\Controller;
  21.  
  22. class LoginController extends Controller
  23. {
  24. public function index(Request $request) {
  25. $return = null;
  26. $this->validate($request, [
  27. 'username' => 'required',
  28. 'password' => 'required',
  29. ]);
  30.  
  31. $params = $request->all();
  32. $user = array(
  33. 'username' => $params['username'],
  34. 'password' => $params['password']
  35. );
  36. if(Auth::attempt($user)) {
  37. User::updateLogin(Auth::user()->id);
  38.  
  39. $row = Auth::user();
  40. $expires = DateTime::createFromFormat(DATE_FORMAT, $row->expires);
  41. $now = new DateTime();
  42.  
  43. // If expired then logout
  44. if($now > $expires){
  45. Auth::logout();
  46.  
  47. return redirect('/login')->with('message', 'Your account reach expired date');
  48. } else {
  49. // Storing session
  50. $categoryTree = $this->getCategoryTree();
  51. Session::put('category', $categoryTree);
  52. Session::put('subcategory', $this->getSubCategory());
  53.  
  54. return redirect()->intended('/dashboard');
  55. }
  56. } else {
  57. return redirect('/login')->with('message', 'Incorrect username and password');
  58. }
  59. }
  60.  
  61. public function resetPassword(Request $request) {
  62. $return = redirect('/forgot')->with('message', "We can't find your email in our database.");
  63.  
  64. // Get email
  65. $email = $request->input('email');
  66.  
  67. $row = User::where('email', $email)->first();
  68.  
  69. if(!empty($row)) {
  70. // Generate token
  71. $token = str_random(32);
  72.  
  73. $row->resetToken = $token;
  74. $row->save();
  75.  
  76. // Send token link to email
  77. Mail::send('resetpassword', ['token' => $token], function ($m) use ($row) {
  78. $email = $row->email;
  79. $m->from('hello@app.com', 'Your Application');
  80.  
  81. $m->to($email, $row->username)->subject('Resetting password!');
  82. });
  83.  
  84. $return = redirect('/login')->with('message', "Reset password link has been sent to your email. Please don't forget to look in your Spam folder.");
  85. }
  86.  
  87. return $return;
  88. }
  89.  
  90. public function reset(Request $request, $token = null) {
  91. if($request->isMethod('post')) {
  92. $params = $request->all();
  93. $token = $params['token'];
  94.  
  95. $return = redirect('/reset//' . $token)->with('message', 'Password has been reset.');
  96.  
  97. $this->validate($request, [
  98. 'token' => 'required',
  99. 'password' => 'required|alpha_num|min:7|confirmed',
  100. 'password_confirmation' => 'required',
  101. ]);
  102.  
  103. // Get user
  104. $user = User::where('resetToken', $token)->first();
  105. if(!empty($user)) {
  106. $user->password = Hash::make($params['password']);
  107. $user->resetToken = null;
  108. $user->save();
  109.  
  110. $return = redirect('/login')->with('message', 'Password has been reset.');
  111. }
  112.  
  113. return $return;
  114. }else{
  115. $user = User::where('resetToken', $token)->first();
  116.  
  117. if(empty($user)) {
  118. return redirect('/login')->with('message', 'Reset token not found.');
  119. }
  120.  
  121. return view('reset', array('token' => $token));
  122. }
  123. }
  124.  
  125. public function logout() {
  126. // Reset all
  127. $this->resetSetting();
  128.  
  129. Auth::logout();
  130.  
  131. return redirect('/login')->with('message', 'Sign out successful');
  132. }
  133.  
  134. // Reset all setting that doesn't need
  135. private function resetSetting() {
  136. $config = Config::where('key_name', 'daterange')->first();
  137. $config->value = null;
  138. $config->save();
  139. }
  140.  
  141. private function getCategoryTree() {
  142. $return = [
  143. // Get all categories
  144. 'category' => Category::getByUserId(Auth::user()->id)->get()->toArray()
  145. ];
  146.  
  147. // Get sub category
  148. if(!empty($return['category'])) {
  149. for($i = 0; $i < count($return['category']); $i++) {
  150. $x = $return['category'][$i];
  151. $subCategory = DB::table('subcategory')->where('category_id', $x['id'])->get();
  152. $subCategory = json_decode(json_encode($subCategory), true);
  153.  
  154. if(!empty($subCategory)) {
  155. $return['category'][$i]['subcategory'] = $subCategory;
  156.  
  157. for($j = 0; $j < count($return['category'][$i]['subcategory']); $j++) {
  158. $x = $return['category'][$i]['subcategory'][$j];
  159.  
  160. $subsubCategory = DB::table('subsubcategory')->where('category_id', $x['id'])->get();
  161. $subsubCategory = json_decode(json_encode($subsubCategory), true);
  162.  
  163. if(!empty($subsubCategory)) {
  164. $return['category'][$i]['subcategory'][$j]['subsubcategory'] = $subsubCategory;
  165. }
  166. }
  167. }
  168. }
  169. }
  170.  
  171. return $return;
  172. }
  173.  
  174. private function getSubCategory() {
  175. $return = [];
  176.  
  177. // Generate all category
  178. $rows = Category::all();
  179. foreach($rows as $x) {
  180. $title = $x->abbrev;
  181.  
  182. $return[] = [
  183. 'id' => $x->id,
  184. 'title' => $title,
  185. 'value' => 'c=' . $x->id
  186. ];
  187. }
  188.  
  189. // Generate all subcategory
  190. $rows = SubCategory::all();
  191. foreach($rows as $x) {
  192. $category = Category::find($x->category_id);
  193.  
  194. $title = $category->abbrev . ' | ' . $x->title;
  195.  
  196. $return[] = [
  197. 'id' => $x->id,
  198. 'title' => $title,
  199. 'value' => 'sc=' . $x->id
  200. ];
  201. }
  202.  
  203. return $return;
  204. }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement