Guest User

Untitled

a guest
Jan 16th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App;
  4.  
  5. use IlluminateNotificationsNotifiable;
  6. use IlluminateContractsAuthMustVerifyEmail;
  7. use IlluminateFoundationAuthUser as Authenticatable;
  8. use LaratrustTraitsLaratrustUserTrait;
  9. use AppNotificationsNewUserPasswordCreate;
  10.  
  11. class User extends Authenticatable
  12. {
  13. use Notifiable;
  14. use LaratrustUserTrait;
  15.  
  16.  
  17. protected $fillable = [
  18. 'name', 'email', 'password',
  19. ];
  20.  
  21.  
  22. protected $hidden = [
  23. 'password', 'remember_token',
  24. ];
  25.  
  26. public function createAcount()
  27. {
  28. $token = app('auth.password.broker')->createToken($this);
  29. return $this->notify(new NewUserPasswordCreate($token));
  30. }
  31. }
  32.  
  33. public function store(Request $request)
  34. {
  35. $this->validate($request, [
  36. 'name' => 'required|min:3|max:255',
  37. 'email' => 'required|unique:users',
  38. 'civilNum' => 'required|size:12',
  39. ]);
  40.  
  41. if ($request->has('password') && !empty($request->password)) {
  42. $this->validate($request, [
  43. 'password' => 'required|min:3|max:255',
  44. ]);
  45. $password = Hash::make($request->password);
  46. } else {
  47. $length = 10;
  48. $keyspace = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
  49. $str= '';
  50. $max = mb_strlen($keyspace, '8bit') - 1;
  51. for ($i=0; $i < $length; ++$i) {
  52. $str .= $keyspace[random_int(0, $max)];
  53. }
  54. $password = $str;
  55. }
  56. $user = new User();
  57. $user->name = $request->name;
  58. $user->email = $request->email;
  59. $user->civilNum = $request->civilNum;
  60. $user->password = $password;
  61.  
  62. $user->password = Hash::make($password);
  63. $user->save();
  64.  
  65. if ($user->save()) {
  66. return redirect()->route('users.index');
  67. } else {
  68. Session::flash('danger', 'Sorry, a problem occured while creating the user.');
  69. return redirect()->route('users.create');
  70. }
  71. }
  72.  
  73. <?php
  74.  
  75. namespace AppNotifications;
  76.  
  77. use IlluminateBusQueueable;
  78. use IlluminateNotificationsNotification;
  79. use IlluminateContractsQueueShouldQueue;
  80. use IlluminateNotificationsMessagesMailMessage;
  81.  
  82. class NewUserPasswordCreate extends Notification
  83. {
  84. use Queueable;
  85.  
  86.  
  87. public function __construct()
  88. {
  89. //
  90. }
  91.  
  92.  
  93. public function via($notifiable)
  94. {
  95. return ['mail'];
  96. }
  97.  
  98.  
  99. public function toMail($notifiable)
  100. {
  101. $link = url( "/password/reset/?token=" . $this->token );
  102.  
  103. return ( new MailMessage )
  104. ->view('reset.emailer')
  105. ->from('info@example.com')
  106. ->subject( 'Reset your password' )
  107. ->line( "Hey, We've successfully changed the text " )
  108. ->action( 'Reset Password', $link )
  109. ->attach('reset.attachment')
  110. ->line( 'Thank you!' );
  111. }
  112.  
  113.  
  114. public function toArray($notifiable)
  115. {
  116. return [
  117. //
  118. ];
  119. }
  120. }
Add Comment
Please, Sign In to add comment