@@ -7,69 +7,69 @@ |
||
| 7 | 7 | |
| 8 | 8 | class Handler extends ExceptionHandler |
| 9 | 9 | {
|
| 10 | - /** |
|
| 11 | - * A list of the exception types that are not reported. |
|
| 12 | - * |
|
| 13 | - * @var array |
|
| 14 | - */ |
|
| 15 | - protected $dontReport = [ |
|
| 16 | - \League\OAuth2\Server\Exception\OAuthServerException::class, |
|
| 17 | - ]; |
|
| 10 | + /** |
|
| 11 | + * A list of the exception types that are not reported. |
|
| 12 | + * |
|
| 13 | + * @var array |
|
| 14 | + */ |
|
| 15 | + protected $dontReport = [ |
|
| 16 | + \League\OAuth2\Server\Exception\OAuthServerException::class, |
|
| 17 | + ]; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * A list of the inputs that are never flashed for validation exceptions. |
|
| 21 | - * |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - protected $dontFlash = [ |
|
| 25 | - 'password', |
|
| 26 | - 'password_confirmation', |
|
| 27 | - ]; |
|
| 19 | + /** |
|
| 20 | + * A list of the inputs that are never flashed for validation exceptions. |
|
| 21 | + * |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + protected $dontFlash = [ |
|
| 25 | + 'password', |
|
| 26 | + 'password_confirmation', |
|
| 27 | + ]; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Report or log an exception. |
|
| 31 | - * |
|
| 32 | - * @param \Throwable $exception |
|
| 33 | - * @return void |
|
| 34 | - * |
|
| 35 | - * @throws \Exception |
|
| 36 | - */ |
|
| 37 | - public function report(Throwable $exception) |
|
| 38 | - {
|
|
| 39 | - parent::report($exception); |
|
| 40 | - } |
|
| 29 | + /** |
|
| 30 | + * Report or log an exception. |
|
| 31 | + * |
|
| 32 | + * @param \Throwable $exception |
|
| 33 | + * @return void |
|
| 34 | + * |
|
| 35 | + * @throws \Exception |
|
| 36 | + */ |
|
| 37 | + public function report(Throwable $exception) |
|
| 38 | + {
|
|
| 39 | + parent::report($exception); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Render an exception into an HTTP response. |
|
| 44 | - * |
|
| 45 | - * @param \Illuminate\Http\Request $request |
|
| 46 | - * @param \Throwable $exception |
|
| 47 | - * @return \Symfony\Component\HttpFoundation\Response |
|
| 48 | - * |
|
| 49 | - * @throws \Throwable |
|
| 50 | - */ |
|
| 51 | - public function render($request, Throwable $exception) |
|
| 52 | - {
|
|
| 53 | - if ($request->wantsJson()) {
|
|
| 54 | - if ($exception instanceof \Illuminate\Auth\AuthenticationException) {
|
|
| 55 | - \Errors::unAuthorized(); |
|
| 56 | - } |
|
| 57 | - if ($exception instanceof \Illuminate\Database\QueryException) {
|
|
| 58 | - \Errors::dbQueryError(); |
|
| 59 | - } elseif ($exception instanceof \predis\connection\connectionexception) {
|
|
| 60 | - \Errors::redisNotRunning(); |
|
| 61 | - } elseif ($exception instanceof \GuzzleHttp\Exception\ClientException) {
|
|
| 62 | - \Errors::connectionError(); |
|
| 63 | - } elseif ($exception instanceof \Symfony\Component\HttpKernel\Exception\HttpException) {
|
|
| 64 | - $errors = $exception-> getStatusCode() === 404 ? 'not found' : $exception-> getMessage(); |
|
| 65 | - return \Response::json(['errors' => [$errors]], $exception-> getStatusCode()); |
|
| 66 | - } elseif ($exception instanceof \Illuminate\Validation\ValidationException) {
|
|
| 67 | - return \Response::json(['errors' => $exception-> errors()], 422); |
|
| 68 | - } elseif (! $exception instanceof \Symfony\Component\ErrorHandler\Error\FatalError) {
|
|
| 69 | - return parent::render($request, $exception); |
|
| 70 | - } |
|
| 71 | - } |
|
| 42 | + /** |
|
| 43 | + * Render an exception into an HTTP response. |
|
| 44 | + * |
|
| 45 | + * @param \Illuminate\Http\Request $request |
|
| 46 | + * @param \Throwable $exception |
|
| 47 | + * @return \Symfony\Component\HttpFoundation\Response |
|
| 48 | + * |
|
| 49 | + * @throws \Throwable |
|
| 50 | + */ |
|
| 51 | + public function render($request, Throwable $exception) |
|
| 52 | + {
|
|
| 53 | + if ($request->wantsJson()) {
|
|
| 54 | + if ($exception instanceof \Illuminate\Auth\AuthenticationException) {
|
|
| 55 | + \Errors::unAuthorized(); |
|
| 56 | + } |
|
| 57 | + if ($exception instanceof \Illuminate\Database\QueryException) {
|
|
| 58 | + \Errors::dbQueryError(); |
|
| 59 | + } elseif ($exception instanceof \predis\connection\connectionexception) {
|
|
| 60 | + \Errors::redisNotRunning(); |
|
| 61 | + } elseif ($exception instanceof \GuzzleHttp\Exception\ClientException) {
|
|
| 62 | + \Errors::connectionError(); |
|
| 63 | + } elseif ($exception instanceof \Symfony\Component\HttpKernel\Exception\HttpException) {
|
|
| 64 | + $errors = $exception-> getStatusCode() === 404 ? 'not found' : $exception-> getMessage(); |
|
| 65 | + return \Response::json(['errors' => [$errors]], $exception-> getStatusCode()); |
|
| 66 | + } elseif ($exception instanceof \Illuminate\Validation\ValidationException) {
|
|
| 67 | + return \Response::json(['errors' => $exception-> errors()], 422); |
|
| 68 | + } elseif (! $exception instanceof \Symfony\Component\ErrorHandler\Error\FatalError) {
|
|
| 69 | + return parent::render($request, $exception); |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - return parent::render($request, $exception); |
|
| 74 | - } |
|
| 73 | + return parent::render($request, $exception); |
|
| 74 | + } |
|
| 75 | 75 | } |
@@ -65,7 +65,7 @@ |
||
| 65 | 65 | return \Response::json(['errors' => [$errors]], $exception-> getStatusCode()); |
| 66 | 66 | } elseif ($exception instanceof \Illuminate\Validation\ValidationException) {
|
| 67 | 67 | return \Response::json(['errors' => $exception-> errors()], 422); |
| 68 | - } elseif (! $exception instanceof \Symfony\Component\ErrorHandler\Error\FatalError) {
|
|
| 68 | + } elseif ( ! $exception instanceof \Symfony\Component\ErrorHandler\Error\FatalError) {
|
|
| 69 | 69 | return parent::render($request, $exception); |
| 70 | 70 | } |
| 71 | 71 | } |
@@ -7,31 +7,31 @@ |
||
| 7 | 7 | |
| 8 | 8 | class AuthServiceProvider extends ServiceProvider |
| 9 | 9 | {
|
| 10 | - /** |
|
| 11 | - * The policy mappings for the application. |
|
| 12 | - * |
|
| 13 | - * @var array |
|
| 14 | - */ |
|
| 15 | - protected $policies = [ |
|
| 16 | - 'App\Model' => 'App\Policies\ModelPolicy', |
|
| 17 | - ]; |
|
| 10 | + /** |
|
| 11 | + * The policy mappings for the application. |
|
| 12 | + * |
|
| 13 | + * @var array |
|
| 14 | + */ |
|
| 15 | + protected $policies = [ |
|
| 16 | + 'App\Model' => 'App\Policies\ModelPolicy', |
|
| 17 | + ]; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Register any authentication / authorization services. |
|
| 21 | - * |
|
| 22 | - * @return void |
|
| 23 | - */ |
|
| 24 | - public function boot() |
|
| 25 | - {
|
|
| 26 | - $this->registerPolicies(); |
|
| 19 | + /** |
|
| 20 | + * Register any authentication / authorization services. |
|
| 21 | + * |
|
| 22 | + * @return void |
|
| 23 | + */ |
|
| 24 | + public function boot() |
|
| 25 | + {
|
|
| 26 | + $this->registerPolicies(); |
|
| 27 | 27 | |
| 28 | - Passport::routes(function ($router) {
|
|
| 29 | - $router->forAuthorization(); |
|
| 30 | - $router->forAccessTokens(); |
|
| 31 | - $router->forPersonalAccessTokens(); |
|
| 32 | - $router->forTransientTokens(); |
|
| 33 | - }); |
|
| 34 | - Passport::tokensExpireIn(\Carbon\Carbon::now()->addMinutes(60)); |
|
| 35 | - Passport::refreshTokensExpireIn(\Carbon\Carbon::now()->addMinutes(14400)); |
|
| 36 | - } |
|
| 28 | + Passport::routes(function ($router) {
|
|
| 29 | + $router->forAuthorization(); |
|
| 30 | + $router->forAccessTokens(); |
|
| 31 | + $router->forPersonalAccessTokens(); |
|
| 32 | + $router->forTransientTokens(); |
|
| 33 | + }); |
|
| 34 | + Passport::tokensExpireIn(\Carbon\Carbon::now()->addMinutes(60)); |
|
| 35 | + Passport::refreshTokensExpireIn(\Carbon\Carbon::now()->addMinutes(14400)); |
|
| 36 | + } |
|
| 37 | 37 | } |
@@ -9,16 +9,16 @@ |
||
| 9 | 9 | class Report extends Model |
| 10 | 10 | { |
| 11 | 11 | |
| 12 | - use SoftDeletes; |
|
| 13 | - protected $table = 'reports'; |
|
| 14 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
| 15 | - protected $hidden = ['deleted_at']; |
|
| 16 | - protected $guarded = ['id']; |
|
| 17 | - public $fillable = ['report_name', 'view_name']; |
|
| 12 | + use SoftDeletes; |
|
| 13 | + protected $table = 'reports'; |
|
| 14 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
| 15 | + protected $hidden = ['deleted_at']; |
|
| 16 | + protected $guarded = ['id']; |
|
| 17 | + public $fillable = ['report_name', 'view_name']; |
|
| 18 | 18 | |
| 19 | - public static function boot() |
|
| 20 | - { |
|
| 21 | - parent::boot(); |
|
| 22 | - Report::observe(ReportObserver::class); |
|
| 23 | - } |
|
| 19 | + public static function boot() |
|
| 20 | + { |
|
| 21 | + parent::boot(); |
|
| 22 | + Report::observe(ReportObserver::class); |
|
| 23 | + } |
|
| 24 | 24 | } |
@@ -5,29 +5,29 @@ |
||
| 5 | 5 | |
| 6 | 6 | class Reports extends Migration |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * Run the migrations. |
|
| 10 | - * |
|
| 11 | - * @return void |
|
| 12 | - */ |
|
| 13 | - public function up() |
|
| 14 | - { |
|
| 15 | - Schema::create('reports', function (Blueprint $table) { |
|
| 16 | - increments('id'); |
|
| 17 | - $table->string('report_name', 100); |
|
| 18 | - $table->string('view_name', 100); |
|
| 19 | - $table->softDeletes(); |
|
| 20 | - $table->timestamps(); |
|
| 21 | - }); |
|
| 22 | - } |
|
| 8 | + /** |
|
| 9 | + * Run the migrations. |
|
| 10 | + * |
|
| 11 | + * @return void |
|
| 12 | + */ |
|
| 13 | + public function up() |
|
| 14 | + { |
|
| 15 | + Schema::create('reports', function (Blueprint $table) { |
|
| 16 | + increments('id'); |
|
| 17 | + $table->string('report_name', 100); |
|
| 18 | + $table->string('view_name', 100); |
|
| 19 | + $table->softDeletes(); |
|
| 20 | + $table->timestamps(); |
|
| 21 | + }); |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Reverse the migrations. |
|
| 26 | - * |
|
| 27 | - * @return void |
|
| 28 | - */ |
|
| 29 | - public function down() |
|
| 30 | - { |
|
| 31 | - Schema::dropIfExists('reports'); |
|
| 32 | - } |
|
| 24 | + /** |
|
| 25 | + * Reverse the migrations. |
|
| 26 | + * |
|
| 27 | + * @return void |
|
| 28 | + */ |
|
| 29 | + public function down() |
|
| 30 | + { |
|
| 31 | + Schema::dropIfExists('reports'); |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -12,7 +12,7 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | public function up() |
| 14 | 14 | { |
| 15 | - Schema::create('reports', function (Blueprint $table) { |
|
| 15 | + Schema::create('reports', function(Blueprint $table) { |
|
| 16 | 16 | increments('id'); |
| 17 | 17 | $table->string('report_name', 100); |
| 18 | 18 | $table->string('view_name', 100); |
@@ -12,112 +12,112 @@ |
||
| 12 | 12 | |
| 13 | 13 | class AclUser extends User |
| 14 | 14 | { |
| 15 | - use SoftDeletes, HasApiTokens; |
|
| 16 | - protected $table = 'users'; |
|
| 17 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
| 18 | - protected $hidden = ['password', 'remember_token', 'deleted_at']; |
|
| 19 | - protected $guarded = ['id']; |
|
| 20 | - public $fillable = ['profile_picture', 'name', 'email', 'password', 'locale', 'timezone']; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * Encrypt the password attribute before |
|
| 24 | - * saving it in the storage. |
|
| 25 | - * |
|
| 26 | - * @param string $value |
|
| 27 | - */ |
|
| 28 | - public function setPasswordAttribute($value) |
|
| 29 | - { |
|
| 30 | - $this->attributes['password'] = \Hash::make($value); |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Get the entity's notifications. |
|
| 35 | - */ |
|
| 36 | - public function notifications() |
|
| 37 | - { |
|
| 38 | - return $this->morphMany(Notification::class, 'notifiable')->orderBy('created_at', 'desc'); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Get the entity's read notifications. |
|
| 43 | - */ |
|
| 44 | - public function readNotifications() |
|
| 45 | - { |
|
| 46 | - return $this->notifications()->whereNotNull('read_at'); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Get the entity's unread notifications. |
|
| 51 | - */ |
|
| 52 | - public function unreadNotifications() |
|
| 53 | - { |
|
| 54 | - return $this->notifications()->whereNull('read_at'); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - public function roles() |
|
| 58 | - { |
|
| 59 | - return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id')->whereNull('role_user.deleted_at')->withTimestamps(); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - public function oauthClients() |
|
| 63 | - { |
|
| 64 | - return $this->hasMany(OauthClient::class, 'user_id'); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - public function setProfilePictureAttribute($value) { |
|
| 68 | - $this->attributes['profile_picture'] = \Media::uploadImageBas64($value, 'users/profile_pictures'); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Return fcm device tokens that will be used in sending fcm notifications. |
|
| 73 | - * |
|
| 74 | - * @return array |
|
| 75 | - */ |
|
| 76 | - public function routeNotificationForFCM() |
|
| 77 | - { |
|
| 78 | - $devices = \Core::pushNotificationDevices()->findBy(['user_id' => $this->id]); |
|
| 79 | - $tokens = []; |
|
| 80 | - |
|
| 81 | - foreach ($devices as $device) { |
|
| 82 | - if (\Core::oauthClients()->accessTokenExpiredOrRevoked($device->access_token)) { |
|
| 83 | - $device->forceDelete(); |
|
| 84 | - continue; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - $tokens[] = $device->device_token; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - return $tokens; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * The channels the user receives notification broadcasts on. |
|
| 95 | - * |
|
| 96 | - * @return string |
|
| 97 | - */ |
|
| 98 | - public function receivesBroadcastNotificationsOn() |
|
| 99 | - { |
|
| 100 | - return 'users.'.$this->id; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Custom password validation. |
|
| 105 | - * |
|
| 106 | - * @param string $password |
|
| 107 | - * @return boolean |
|
| 108 | - */ |
|
| 109 | - public function validateForPassportPasswordGrant($password) |
|
| 110 | - { |
|
| 111 | - if ($password == config('user.social_pass')) { |
|
| 112 | - return true; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - return \Hash::check($password, $this->password); |
|
| 116 | - } |
|
| 15 | + use SoftDeletes, HasApiTokens; |
|
| 16 | + protected $table = 'users'; |
|
| 17 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
| 18 | + protected $hidden = ['password', 'remember_token', 'deleted_at']; |
|
| 19 | + protected $guarded = ['id']; |
|
| 20 | + public $fillable = ['profile_picture', 'name', 'email', 'password', 'locale', 'timezone']; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * Encrypt the password attribute before |
|
| 24 | + * saving it in the storage. |
|
| 25 | + * |
|
| 26 | + * @param string $value |
|
| 27 | + */ |
|
| 28 | + public function setPasswordAttribute($value) |
|
| 29 | + { |
|
| 30 | + $this->attributes['password'] = \Hash::make($value); |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Get the entity's notifications. |
|
| 35 | + */ |
|
| 36 | + public function notifications() |
|
| 37 | + { |
|
| 38 | + return $this->morphMany(Notification::class, 'notifiable')->orderBy('created_at', 'desc'); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Get the entity's read notifications. |
|
| 43 | + */ |
|
| 44 | + public function readNotifications() |
|
| 45 | + { |
|
| 46 | + return $this->notifications()->whereNotNull('read_at'); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Get the entity's unread notifications. |
|
| 51 | + */ |
|
| 52 | + public function unreadNotifications() |
|
| 53 | + { |
|
| 54 | + return $this->notifications()->whereNull('read_at'); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + public function roles() |
|
| 58 | + { |
|
| 59 | + return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id')->whereNull('role_user.deleted_at')->withTimestamps(); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + public function oauthClients() |
|
| 63 | + { |
|
| 64 | + return $this->hasMany(OauthClient::class, 'user_id'); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + public function setProfilePictureAttribute($value) { |
|
| 68 | + $this->attributes['profile_picture'] = \Media::uploadImageBas64($value, 'users/profile_pictures'); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Return fcm device tokens that will be used in sending fcm notifications. |
|
| 73 | + * |
|
| 74 | + * @return array |
|
| 75 | + */ |
|
| 76 | + public function routeNotificationForFCM() |
|
| 77 | + { |
|
| 78 | + $devices = \Core::pushNotificationDevices()->findBy(['user_id' => $this->id]); |
|
| 79 | + $tokens = []; |
|
| 80 | + |
|
| 81 | + foreach ($devices as $device) { |
|
| 82 | + if (\Core::oauthClients()->accessTokenExpiredOrRevoked($device->access_token)) { |
|
| 83 | + $device->forceDelete(); |
|
| 84 | + continue; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + $tokens[] = $device->device_token; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + return $tokens; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * The channels the user receives notification broadcasts on. |
|
| 95 | + * |
|
| 96 | + * @return string |
|
| 97 | + */ |
|
| 98 | + public function receivesBroadcastNotificationsOn() |
|
| 99 | + { |
|
| 100 | + return 'users.'.$this->id; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Custom password validation. |
|
| 105 | + * |
|
| 106 | + * @param string $password |
|
| 107 | + * @return boolean |
|
| 108 | + */ |
|
| 109 | + public function validateForPassportPasswordGrant($password) |
|
| 110 | + { |
|
| 111 | + if ($password == config('user.social_pass')) { |
|
| 112 | + return true; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + return \Hash::check($password, $this->password); |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | - public static function boot() |
|
| 119 | - { |
|
| 120 | - parent::boot(); |
|
| 121 | - AclUser::observe(AclUserObserver::class); |
|
| 122 | - } |
|
| 118 | + public static function boot() |
|
| 119 | + { |
|
| 120 | + parent::boot(); |
|
| 121 | + AclUser::observe(AclUserObserver::class); |
|
| 122 | + } |
|
| 123 | 123 | } |
@@ -5,37 +5,37 @@ |
||
| 5 | 5 | |
| 6 | 6 | class Users extends Migration |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * Run the migrations. |
|
| 10 | - * |
|
| 11 | - * @return void |
|
| 12 | - */ |
|
| 13 | - public function up() |
|
| 14 | - { |
|
| 15 | - Schema::create('users', function (Blueprint $table) { |
|
| 16 | - increments('id'); |
|
| 17 | - $table->string('profile_picture', 150)->nullable(); |
|
| 18 | - $table->string('name', 100)->nullable(); |
|
| 19 | - $table->string('email')->unique(); |
|
| 20 | - $table->string('password', 60)->nullable(); |
|
| 21 | - $table->boolean('blocked')->default(0); |
|
| 22 | - $table->boolean('confirmed')->default(0); |
|
| 23 | - $table->string('confirmation_code')->nullable(); |
|
| 24 | - $table->string('locale', 2)->default('en'); |
|
| 25 | - $table->string('timezone', 50)->default('Africa/Cairo'); |
|
| 26 | - $table->softDeletes(); |
|
| 27 | - $table->rememberToken(); |
|
| 28 | - $table->timestamps(); |
|
| 29 | - }); |
|
| 30 | - } |
|
| 8 | + /** |
|
| 9 | + * Run the migrations. |
|
| 10 | + * |
|
| 11 | + * @return void |
|
| 12 | + */ |
|
| 13 | + public function up() |
|
| 14 | + { |
|
| 15 | + Schema::create('users', function (Blueprint $table) { |
|
| 16 | + increments('id'); |
|
| 17 | + $table->string('profile_picture', 150)->nullable(); |
|
| 18 | + $table->string('name', 100)->nullable(); |
|
| 19 | + $table->string('email')->unique(); |
|
| 20 | + $table->string('password', 60)->nullable(); |
|
| 21 | + $table->boolean('blocked')->default(0); |
|
| 22 | + $table->boolean('confirmed')->default(0); |
|
| 23 | + $table->string('confirmation_code')->nullable(); |
|
| 24 | + $table->string('locale', 2)->default('en'); |
|
| 25 | + $table->string('timezone', 50)->default('Africa/Cairo'); |
|
| 26 | + $table->softDeletes(); |
|
| 27 | + $table->rememberToken(); |
|
| 28 | + $table->timestamps(); |
|
| 29 | + }); |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Reverse the migrations. |
|
| 34 | - * |
|
| 35 | - * @return void |
|
| 36 | - */ |
|
| 37 | - public function down() |
|
| 38 | - { |
|
| 39 | - Schema::dropIfExists('users'); |
|
| 40 | - } |
|
| 32 | + /** |
|
| 33 | + * Reverse the migrations. |
|
| 34 | + * |
|
| 35 | + * @return void |
|
| 36 | + */ |
|
| 37 | + public function down() |
|
| 38 | + { |
|
| 39 | + Schema::dropIfExists('users'); |
|
| 40 | + } |
|
| 41 | 41 | } |
@@ -12,7 +12,7 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | public function up() |
| 14 | 14 | { |
| 15 | - Schema::create('users', function (Blueprint $table) { |
|
| 15 | + Schema::create('users', function(Blueprint $table) { |
|
| 16 | 16 | increments('id'); |
| 17 | 17 | $table->string('profile_picture', 150)->nullable(); |
| 18 | 18 | $table->string('name', 100)->nullable(); |
@@ -12,368 +12,368 @@ |
||
| 12 | 12 | |
| 13 | 13 | class UserService extends BaseService |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * @var PermissionService |
|
| 17 | - */ |
|
| 18 | - protected $permissionService; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * @var LoginProxy |
|
| 22 | - */ |
|
| 23 | - protected $loginProxy; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @var NotificationService |
|
| 27 | - */ |
|
| 28 | - protected $notificationService; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var OauthClientService |
|
| 32 | - */ |
|
| 33 | - protected $oauthClientService; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Init new object. |
|
| 37 | - * |
|
| 38 | - * @param UserRepository $repo |
|
| 39 | - * @param PermissionService $permissionService |
|
| 40 | - * @param LoginProxy $loginProxy |
|
| 41 | - * @param NotificationService $notificationService |
|
| 42 | - * @param OauthClientService $oauthClientService |
|
| 43 | - * @return void |
|
| 44 | - */ |
|
| 45 | - public function __construct( |
|
| 46 | - UserRepository $repo, |
|
| 47 | - PermissionService $permissionService, |
|
| 48 | - LoginProxy $loginProxy, |
|
| 49 | - NotificationService $notificationService, |
|
| 50 | - OauthClientService $oauthClientService |
|
| 51 | - ) { |
|
| 52 | - $this->permissionService = $permissionService; |
|
| 53 | - $this->loginProxy = $loginProxy; |
|
| 54 | - $this->notificationService = $notificationService; |
|
| 55 | - $this->oauthClientService = $oauthClientService; |
|
| 56 | - parent::__construct($repo); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Return the logged in user account. |
|
| 61 | - * |
|
| 62 | - * @param array $relations |
|
| 63 | - * @return boolean |
|
| 64 | - */ |
|
| 65 | - public function account($relations = ['roles.permissions']) |
|
| 66 | - { |
|
| 67 | - $permissions = []; |
|
| 68 | - $user = $this->repo->find(\Auth::id(), $relations); |
|
| 69 | - foreach ($user->roles as $role) { |
|
| 70 | - $role->permissions->each(function ($permission) use (&$permissions) { |
|
| 71 | - $permissions[$permission->repo][$permission->id] = $permission->name; |
|
| 72 | - }); |
|
| 73 | - } |
|
| 74 | - $user->permissions = $permissions; |
|
| 75 | - |
|
| 76 | - return $user; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Check if the logged in user or the given user |
|
| 81 | - * has the given permissions on the given model. |
|
| 82 | - * |
|
| 83 | - * @param string $permissionName |
|
| 84 | - * @param string $model |
|
| 85 | - * @param mixed $userId |
|
| 86 | - * @return boolean |
|
| 87 | - */ |
|
| 88 | - public function can($permissionName, $model, $userId = false) |
|
| 89 | - { |
|
| 90 | - $permission = $this->permissionService->first([ |
|
| 91 | - 'and' => [ |
|
| 92 | - 'model' => $model, |
|
| 93 | - 'name' => $permissionName, |
|
| 94 | - 'roles' => [ |
|
| 95 | - 'op' => 'has', |
|
| 96 | - 'val' => [ |
|
| 97 | - 'users' => [ |
|
| 98 | - 'op' => 'has', |
|
| 99 | - 'val' => [ |
|
| 100 | - 'users.id' => $userId ?: \Auth::id() |
|
| 101 | - ] |
|
| 102 | - ] |
|
| 103 | - ] |
|
| 104 | - ] |
|
| 105 | - ] |
|
| 106 | - ]); |
|
| 107 | - |
|
| 108 | - return $permission ? true : false; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Check if the logged in or the given user has the given role. |
|
| 113 | - * |
|
| 114 | - * @param string[] $roles |
|
| 115 | - * @param mixed $user |
|
| 116 | - * @return boolean |
|
| 117 | - */ |
|
| 118 | - public function hasRoles($roles, $user = false) |
|
| 119 | - { |
|
| 120 | - return $this->repo->countRoles($user ?: \Auth::id(), $roles) ? true : false; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Assign the given role ids to the given user. |
|
| 125 | - * |
|
| 126 | - * @param integer $userId |
|
| 127 | - * @param array $roleIds |
|
| 128 | - * @return object |
|
| 129 | - */ |
|
| 130 | - public function assignRoles($userId, $roleIds) |
|
| 131 | - { |
|
| 132 | - $user = false; |
|
| 133 | - \DB::transaction(function () use ($userId, $roleIds, &$user) { |
|
| 134 | - $user = $this->repo->find($userId); |
|
| 135 | - $this->repo->detachPermissions($userId); |
|
| 136 | - $this->repo->attachPermissions($userId, $roleIds); |
|
| 137 | - }); |
|
| 138 | - |
|
| 139 | - return $user; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Handle the login request to the application. |
|
| 144 | - * |
|
| 145 | - * @param string $email |
|
| 146 | - * @param string $password |
|
| 147 | - * @return object |
|
| 148 | - */ |
|
| 149 | - public function login($email, $password) |
|
| 150 | - { |
|
| 151 | - if (! $user = $this->repo->first(['email' => $email])) { |
|
| 152 | - \Errors::loginFailed(); |
|
| 153 | - } elseif ($user->blocked) { |
|
| 154 | - \Errors::userIsBlocked(); |
|
| 155 | - } elseif (! config('user.disable_confirm_email') && ! $user->confirmed) { |
|
| 156 | - \Errors::emailNotConfirmed(); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - return ['user' => $user, 'tokens' => $this->loginProxy->login($user->email, $password)]; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Handle the social login request to the application. |
|
| 164 | - * |
|
| 165 | - * @param string $authCode |
|
| 166 | - * @param string $accessToken |
|
| 167 | - * @return array |
|
| 168 | - */ |
|
| 169 | - public function loginSocial($authCode, $accessToken, $type) |
|
| 170 | - { |
|
| 171 | - $access_token = $authCode ? Arr::get(\Socialite::driver($type)->getAccessTokenResponse($authCode), 'access_token') : $accessToken; |
|
| 172 | - $user = \Socialite::driver($type)->userFromToken($access_token); |
|
| 173 | - |
|
| 174 | - if (! $user->email) { |
|
| 175 | - \Errors::noSocialEmail(); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - if (! $this->repo->first(['email' => $user->email])) { |
|
| 179 | - $this->register($user->email, '', true); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - return $this->loginProxy->login($user->email, config('user.social_pass')); |
|
| 183 | - } |
|
| 15 | + /** |
|
| 16 | + * @var PermissionService |
|
| 17 | + */ |
|
| 18 | + protected $permissionService; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * @var LoginProxy |
|
| 22 | + */ |
|
| 23 | + protected $loginProxy; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @var NotificationService |
|
| 27 | + */ |
|
| 28 | + protected $notificationService; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var OauthClientService |
|
| 32 | + */ |
|
| 33 | + protected $oauthClientService; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Init new object. |
|
| 37 | + * |
|
| 38 | + * @param UserRepository $repo |
|
| 39 | + * @param PermissionService $permissionService |
|
| 40 | + * @param LoginProxy $loginProxy |
|
| 41 | + * @param NotificationService $notificationService |
|
| 42 | + * @param OauthClientService $oauthClientService |
|
| 43 | + * @return void |
|
| 44 | + */ |
|
| 45 | + public function __construct( |
|
| 46 | + UserRepository $repo, |
|
| 47 | + PermissionService $permissionService, |
|
| 48 | + LoginProxy $loginProxy, |
|
| 49 | + NotificationService $notificationService, |
|
| 50 | + OauthClientService $oauthClientService |
|
| 51 | + ) { |
|
| 52 | + $this->permissionService = $permissionService; |
|
| 53 | + $this->loginProxy = $loginProxy; |
|
| 54 | + $this->notificationService = $notificationService; |
|
| 55 | + $this->oauthClientService = $oauthClientService; |
|
| 56 | + parent::__construct($repo); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Return the logged in user account. |
|
| 61 | + * |
|
| 62 | + * @param array $relations |
|
| 63 | + * @return boolean |
|
| 64 | + */ |
|
| 65 | + public function account($relations = ['roles.permissions']) |
|
| 66 | + { |
|
| 67 | + $permissions = []; |
|
| 68 | + $user = $this->repo->find(\Auth::id(), $relations); |
|
| 69 | + foreach ($user->roles as $role) { |
|
| 70 | + $role->permissions->each(function ($permission) use (&$permissions) { |
|
| 71 | + $permissions[$permission->repo][$permission->id] = $permission->name; |
|
| 72 | + }); |
|
| 73 | + } |
|
| 74 | + $user->permissions = $permissions; |
|
| 75 | + |
|
| 76 | + return $user; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Check if the logged in user or the given user |
|
| 81 | + * has the given permissions on the given model. |
|
| 82 | + * |
|
| 83 | + * @param string $permissionName |
|
| 84 | + * @param string $model |
|
| 85 | + * @param mixed $userId |
|
| 86 | + * @return boolean |
|
| 87 | + */ |
|
| 88 | + public function can($permissionName, $model, $userId = false) |
|
| 89 | + { |
|
| 90 | + $permission = $this->permissionService->first([ |
|
| 91 | + 'and' => [ |
|
| 92 | + 'model' => $model, |
|
| 93 | + 'name' => $permissionName, |
|
| 94 | + 'roles' => [ |
|
| 95 | + 'op' => 'has', |
|
| 96 | + 'val' => [ |
|
| 97 | + 'users' => [ |
|
| 98 | + 'op' => 'has', |
|
| 99 | + 'val' => [ |
|
| 100 | + 'users.id' => $userId ?: \Auth::id() |
|
| 101 | + ] |
|
| 102 | + ] |
|
| 103 | + ] |
|
| 104 | + ] |
|
| 105 | + ] |
|
| 106 | + ]); |
|
| 107 | + |
|
| 108 | + return $permission ? true : false; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Check if the logged in or the given user has the given role. |
|
| 113 | + * |
|
| 114 | + * @param string[] $roles |
|
| 115 | + * @param mixed $user |
|
| 116 | + * @return boolean |
|
| 117 | + */ |
|
| 118 | + public function hasRoles($roles, $user = false) |
|
| 119 | + { |
|
| 120 | + return $this->repo->countRoles($user ?: \Auth::id(), $roles) ? true : false; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Assign the given role ids to the given user. |
|
| 125 | + * |
|
| 126 | + * @param integer $userId |
|
| 127 | + * @param array $roleIds |
|
| 128 | + * @return object |
|
| 129 | + */ |
|
| 130 | + public function assignRoles($userId, $roleIds) |
|
| 131 | + { |
|
| 132 | + $user = false; |
|
| 133 | + \DB::transaction(function () use ($userId, $roleIds, &$user) { |
|
| 134 | + $user = $this->repo->find($userId); |
|
| 135 | + $this->repo->detachPermissions($userId); |
|
| 136 | + $this->repo->attachPermissions($userId, $roleIds); |
|
| 137 | + }); |
|
| 138 | + |
|
| 139 | + return $user; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Handle the login request to the application. |
|
| 144 | + * |
|
| 145 | + * @param string $email |
|
| 146 | + * @param string $password |
|
| 147 | + * @return object |
|
| 148 | + */ |
|
| 149 | + public function login($email, $password) |
|
| 150 | + { |
|
| 151 | + if (! $user = $this->repo->first(['email' => $email])) { |
|
| 152 | + \Errors::loginFailed(); |
|
| 153 | + } elseif ($user->blocked) { |
|
| 154 | + \Errors::userIsBlocked(); |
|
| 155 | + } elseif (! config('user.disable_confirm_email') && ! $user->confirmed) { |
|
| 156 | + \Errors::emailNotConfirmed(); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + return ['user' => $user, 'tokens' => $this->loginProxy->login($user->email, $password)]; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Handle the social login request to the application. |
|
| 164 | + * |
|
| 165 | + * @param string $authCode |
|
| 166 | + * @param string $accessToken |
|
| 167 | + * @return array |
|
| 168 | + */ |
|
| 169 | + public function loginSocial($authCode, $accessToken, $type) |
|
| 170 | + { |
|
| 171 | + $access_token = $authCode ? Arr::get(\Socialite::driver($type)->getAccessTokenResponse($authCode), 'access_token') : $accessToken; |
|
| 172 | + $user = \Socialite::driver($type)->userFromToken($access_token); |
|
| 173 | + |
|
| 174 | + if (! $user->email) { |
|
| 175 | + \Errors::noSocialEmail(); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + if (! $this->repo->first(['email' => $user->email])) { |
|
| 179 | + $this->register($user->email, '', true); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + return $this->loginProxy->login($user->email, config('user.social_pass')); |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - /** |
|
| 186 | - * Handle the registration request. |
|
| 187 | - * |
|
| 188 | - * @param string $name |
|
| 189 | - * @param string $email |
|
| 190 | - * @param string $password |
|
| 191 | - * @param boolean $skipConfirmEmail |
|
| 192 | - * @return array |
|
| 193 | - */ |
|
| 194 | - public function register($name, $email, $password, $skipConfirmEmail = false) |
|
| 195 | - { |
|
| 196 | - $user = $this->repo->save([ |
|
| 197 | - 'name' => $name, |
|
| 198 | - 'email' => $email, |
|
| 199 | - 'password' => $password, |
|
| 200 | - 'confirmed' => $skipConfirmEmail |
|
| 201 | - ]); |
|
| 202 | - |
|
| 203 | - if (! $skipConfirmEmail && ! config('user.disable_confirm_email')) { |
|
| 204 | - $this->sendConfirmationEmail($user->email); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - return $user; |
|
| 208 | - } |
|
| 185 | + /** |
|
| 186 | + * Handle the registration request. |
|
| 187 | + * |
|
| 188 | + * @param string $name |
|
| 189 | + * @param string $email |
|
| 190 | + * @param string $password |
|
| 191 | + * @param boolean $skipConfirmEmail |
|
| 192 | + * @return array |
|
| 193 | + */ |
|
| 194 | + public function register($name, $email, $password, $skipConfirmEmail = false) |
|
| 195 | + { |
|
| 196 | + $user = $this->repo->save([ |
|
| 197 | + 'name' => $name, |
|
| 198 | + 'email' => $email, |
|
| 199 | + 'password' => $password, |
|
| 200 | + 'confirmed' => $skipConfirmEmail |
|
| 201 | + ]); |
|
| 202 | + |
|
| 203 | + if (! $skipConfirmEmail && ! config('user.disable_confirm_email')) { |
|
| 204 | + $this->sendConfirmationEmail($user->email); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + return $user; |
|
| 208 | + } |
|
| 209 | 209 | |
| 210 | - /** |
|
| 211 | - * Block the user. |
|
| 212 | - * |
|
| 213 | - * @param integer $userId |
|
| 214 | - * @return object |
|
| 215 | - */ |
|
| 216 | - public function block($userId) |
|
| 217 | - { |
|
| 218 | - if (\Auth::id() == $userId) { |
|
| 219 | - \Errors::noPermissions(); |
|
| 220 | - } |
|
| 210 | + /** |
|
| 211 | + * Block the user. |
|
| 212 | + * |
|
| 213 | + * @param integer $userId |
|
| 214 | + * @return object |
|
| 215 | + */ |
|
| 216 | + public function block($userId) |
|
| 217 | + { |
|
| 218 | + if (\Auth::id() == $userId) { |
|
| 219 | + \Errors::noPermissions(); |
|
| 220 | + } |
|
| 221 | 221 | |
| 222 | - return $this->repo->save(['id' => $userId, 'blocked' => 1]); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Unblock the user. |
|
| 227 | - * |
|
| 228 | - * @param integer $userId |
|
| 229 | - * @return object |
|
| 230 | - */ |
|
| 231 | - public function unblock($userId) |
|
| 232 | - { |
|
| 233 | - return $this->repo->save(['id' => $userId, 'blocked' => 0]); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * Send a reset link to the given user. |
|
| 238 | - * |
|
| 239 | - * @param string $email |
|
| 240 | - * @return void |
|
| 241 | - */ |
|
| 242 | - public function sendReset($email) |
|
| 243 | - { |
|
| 244 | - if (! $user = $this->repo->first(['email' => $email])) { |
|
| 245 | - \Errors::notFound('email'); |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - $token = \Password::createToken($user); |
|
| 249 | - $this->notificationService->notify($user, 'ResetPassword', $token); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Reset the given user's password. |
|
| 254 | - * |
|
| 255 | - * @param string $email |
|
| 256 | - * @param string $password |
|
| 257 | - * @param string $passwordConfirmation |
|
| 258 | - * @param string $token |
|
| 259 | - * @return string|void |
|
| 260 | - */ |
|
| 261 | - public function resetPassword($email, $password, $passwordConfirmation, $token) |
|
| 262 | - { |
|
| 263 | - $response = \Password::reset([ |
|
| 264 | - 'email' => $email, |
|
| 265 | - 'password' => $password, |
|
| 266 | - 'password_confirmation' => $passwordConfirmation, |
|
| 267 | - 'token' => $token |
|
| 268 | - ], function ($user, $password) { |
|
| 269 | - $this->repo->save(['id' => $user->id, 'password' => $password]); |
|
| 270 | - }); |
|
| 271 | - |
|
| 272 | - switch ($response) { |
|
| 273 | - case \Password::PASSWORD_RESET: |
|
| 274 | - return 'success'; |
|
| 275 | - break; |
|
| 276 | - |
|
| 277 | - case \Password::INVALID_TOKEN: |
|
| 278 | - \Errors::invalidResetToken(); |
|
| 279 | - break; |
|
| 280 | - |
|
| 281 | - case \Password::INVALID_PASSWORD: |
|
| 282 | - \Errors::invalidResetPassword(); |
|
| 283 | - break; |
|
| 284 | - |
|
| 285 | - case \Password::INVALID_USER: |
|
| 286 | - \Errors::notFound('user'); |
|
| 287 | - break; |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * Change the logged in user password. |
|
| 293 | - * |
|
| 294 | - * @param string $password |
|
| 295 | - * @param string $oldPassword |
|
| 296 | - * @return void |
|
| 297 | - */ |
|
| 298 | - public function changePassword($password, $oldPassword) |
|
| 299 | - { |
|
| 300 | - $user = \Auth::user(); |
|
| 301 | - if (! \Hash::check($oldPassword, $user->password)) { |
|
| 302 | - \Errors::invalidOldPassword(); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - $this->repo->save(['id' => $user->id, 'password' => $password]); |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * Confirm email using the confirmation code. |
|
| 310 | - * |
|
| 311 | - * @param string $confirmationCode |
|
| 312 | - * @return void |
|
| 313 | - */ |
|
| 314 | - public function confirmEmail($confirmationCode) |
|
| 315 | - { |
|
| 316 | - if (! $user = $this->repo->first(['confirmation_code' => $confirmationCode])) { |
|
| 317 | - \Errors::invalidConfirmationCode(); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - $this->repo->save(['id' => $user->id, 'confirmed' => 1, 'confirmation_code' => null]); |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * Send the confirmation mail. |
|
| 325 | - * |
|
| 326 | - * @param string $email |
|
| 327 | - * @return void |
|
| 328 | - */ |
|
| 329 | - public function sendConfirmationEmail($email) |
|
| 330 | - { |
|
| 331 | - $user = $this->repo->first(['email' => $email]); |
|
| 332 | - if ($user->confirmed) { |
|
| 333 | - \Errors::emailAlreadyConfirmed(); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - $this->repo->save(['id' => $user->id, 'confirmation_code' => sha1(microtime())]); |
|
| 337 | - $this->notificationService->notify($user, 'ConfirmEmail'); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * Save the given data to the logged in user. |
|
| 342 | - * |
|
| 343 | - * @param string $name |
|
| 344 | - * @param string $email |
|
| 345 | - * @param string $profilePicture |
|
| 346 | - * @return void |
|
| 347 | - */ |
|
| 348 | - public function saveProfile($name, $email, $profilePicture = false) |
|
| 349 | - { |
|
| 350 | - $data['id'] = \Auth::id(); |
|
| 351 | - return $this->repo->save([ |
|
| 352 | - 'id' => \Auth::id(), |
|
| 353 | - 'name' => $name, |
|
| 354 | - 'email' => $email, |
|
| 355 | - 'profilePicture' => $profilePicture, |
|
| 356 | - ]); |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * Logs out the user, revoke access token and refresh token. |
|
| 361 | - * |
|
| 362 | - * @return void |
|
| 363 | - */ |
|
| 364 | - public function logout() |
|
| 365 | - { |
|
| 366 | - $this->oauthClientService->revokeAccessToken(\Auth::user()->token()); |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Attempt to refresh the access token using the given refresh token. |
|
| 371 | - * |
|
| 372 | - * @param string $refreshToken |
|
| 373 | - * @return array |
|
| 374 | - */ |
|
| 375 | - public function refreshToken($refreshToken) |
|
| 376 | - { |
|
| 377 | - return $this->loginProxy->refreshToken($refreshToken); |
|
| 378 | - } |
|
| 222 | + return $this->repo->save(['id' => $userId, 'blocked' => 1]); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Unblock the user. |
|
| 227 | + * |
|
| 228 | + * @param integer $userId |
|
| 229 | + * @return object |
|
| 230 | + */ |
|
| 231 | + public function unblock($userId) |
|
| 232 | + { |
|
| 233 | + return $this->repo->save(['id' => $userId, 'blocked' => 0]); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * Send a reset link to the given user. |
|
| 238 | + * |
|
| 239 | + * @param string $email |
|
| 240 | + * @return void |
|
| 241 | + */ |
|
| 242 | + public function sendReset($email) |
|
| 243 | + { |
|
| 244 | + if (! $user = $this->repo->first(['email' => $email])) { |
|
| 245 | + \Errors::notFound('email'); |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + $token = \Password::createToken($user); |
|
| 249 | + $this->notificationService->notify($user, 'ResetPassword', $token); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Reset the given user's password. |
|
| 254 | + * |
|
| 255 | + * @param string $email |
|
| 256 | + * @param string $password |
|
| 257 | + * @param string $passwordConfirmation |
|
| 258 | + * @param string $token |
|
| 259 | + * @return string|void |
|
| 260 | + */ |
|
| 261 | + public function resetPassword($email, $password, $passwordConfirmation, $token) |
|
| 262 | + { |
|
| 263 | + $response = \Password::reset([ |
|
| 264 | + 'email' => $email, |
|
| 265 | + 'password' => $password, |
|
| 266 | + 'password_confirmation' => $passwordConfirmation, |
|
| 267 | + 'token' => $token |
|
| 268 | + ], function ($user, $password) { |
|
| 269 | + $this->repo->save(['id' => $user->id, 'password' => $password]); |
|
| 270 | + }); |
|
| 271 | + |
|
| 272 | + switch ($response) { |
|
| 273 | + case \Password::PASSWORD_RESET: |
|
| 274 | + return 'success'; |
|
| 275 | + break; |
|
| 276 | + |
|
| 277 | + case \Password::INVALID_TOKEN: |
|
| 278 | + \Errors::invalidResetToken(); |
|
| 279 | + break; |
|
| 280 | + |
|
| 281 | + case \Password::INVALID_PASSWORD: |
|
| 282 | + \Errors::invalidResetPassword(); |
|
| 283 | + break; |
|
| 284 | + |
|
| 285 | + case \Password::INVALID_USER: |
|
| 286 | + \Errors::notFound('user'); |
|
| 287 | + break; |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * Change the logged in user password. |
|
| 293 | + * |
|
| 294 | + * @param string $password |
|
| 295 | + * @param string $oldPassword |
|
| 296 | + * @return void |
|
| 297 | + */ |
|
| 298 | + public function changePassword($password, $oldPassword) |
|
| 299 | + { |
|
| 300 | + $user = \Auth::user(); |
|
| 301 | + if (! \Hash::check($oldPassword, $user->password)) { |
|
| 302 | + \Errors::invalidOldPassword(); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + $this->repo->save(['id' => $user->id, 'password' => $password]); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * Confirm email using the confirmation code. |
|
| 310 | + * |
|
| 311 | + * @param string $confirmationCode |
|
| 312 | + * @return void |
|
| 313 | + */ |
|
| 314 | + public function confirmEmail($confirmationCode) |
|
| 315 | + { |
|
| 316 | + if (! $user = $this->repo->first(['confirmation_code' => $confirmationCode])) { |
|
| 317 | + \Errors::invalidConfirmationCode(); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + $this->repo->save(['id' => $user->id, 'confirmed' => 1, 'confirmation_code' => null]); |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * Send the confirmation mail. |
|
| 325 | + * |
|
| 326 | + * @param string $email |
|
| 327 | + * @return void |
|
| 328 | + */ |
|
| 329 | + public function sendConfirmationEmail($email) |
|
| 330 | + { |
|
| 331 | + $user = $this->repo->first(['email' => $email]); |
|
| 332 | + if ($user->confirmed) { |
|
| 333 | + \Errors::emailAlreadyConfirmed(); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + $this->repo->save(['id' => $user->id, 'confirmation_code' => sha1(microtime())]); |
|
| 337 | + $this->notificationService->notify($user, 'ConfirmEmail'); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * Save the given data to the logged in user. |
|
| 342 | + * |
|
| 343 | + * @param string $name |
|
| 344 | + * @param string $email |
|
| 345 | + * @param string $profilePicture |
|
| 346 | + * @return void |
|
| 347 | + */ |
|
| 348 | + public function saveProfile($name, $email, $profilePicture = false) |
|
| 349 | + { |
|
| 350 | + $data['id'] = \Auth::id(); |
|
| 351 | + return $this->repo->save([ |
|
| 352 | + 'id' => \Auth::id(), |
|
| 353 | + 'name' => $name, |
|
| 354 | + 'email' => $email, |
|
| 355 | + 'profilePicture' => $profilePicture, |
|
| 356 | + ]); |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * Logs out the user, revoke access token and refresh token. |
|
| 361 | + * |
|
| 362 | + * @return void |
|
| 363 | + */ |
|
| 364 | + public function logout() |
|
| 365 | + { |
|
| 366 | + $this->oauthClientService->revokeAccessToken(\Auth::user()->token()); |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Attempt to refresh the access token using the given refresh token. |
|
| 371 | + * |
|
| 372 | + * @param string $refreshToken |
|
| 373 | + * @return array |
|
| 374 | + */ |
|
| 375 | + public function refreshToken($refreshToken) |
|
| 376 | + { |
|
| 377 | + return $this->loginProxy->refreshToken($refreshToken); |
|
| 378 | + } |
|
| 379 | 379 | } |
@@ -15,29 +15,29 @@ |
||
| 15 | 15 | |
| 16 | 16 | Route::group(['prefix' => 'users'], function () { |
| 17 | 17 | |
| 18 | - Route::get('/', 'UserController@index'); |
|
| 19 | - Route::get('{id}', 'UserController@show'); |
|
| 20 | - Route::post('/', 'UserController@store'); |
|
| 21 | - Route::patch('{id}', 'UserController@update'); |
|
| 22 | - Route::delete('{id}', 'UserController@destroy'); |
|
| 23 | - Route::patch('restore/{id}', 'UserController@restore'); |
|
| 24 | - Route::patch('block/{id}', 'UserController@block'); |
|
| 25 | - Route::patch('unblock/{id}', 'UserController@unblock'); |
|
| 26 | - Route::patch('assign/roles/{id}', 'UserController@assignRoles'); |
|
| 18 | + Route::get('/', 'UserController@index'); |
|
| 19 | + Route::get('{id}', 'UserController@show'); |
|
| 20 | + Route::post('/', 'UserController@store'); |
|
| 21 | + Route::patch('{id}', 'UserController@update'); |
|
| 22 | + Route::delete('{id}', 'UserController@destroy'); |
|
| 23 | + Route::patch('restore/{id}', 'UserController@restore'); |
|
| 24 | + Route::patch('block/{id}', 'UserController@block'); |
|
| 25 | + Route::patch('unblock/{id}', 'UserController@unblock'); |
|
| 26 | + Route::patch('assign/roles/{id}', 'UserController@assignRoles'); |
|
| 27 | 27 | |
| 28 | - Route::group(['prefix' => 'account'], function () { |
|
| 28 | + Route::group(['prefix' => 'account'], function () { |
|
| 29 | 29 | |
| 30 | - Route::get('my', 'UserController@account'); |
|
| 31 | - Route::get('logout', 'UserController@logout'); |
|
| 32 | - Route::post('refresh/token', 'UserController@refreshToken'); |
|
| 33 | - Route::post('save', 'UserController@saveProfile'); |
|
| 34 | - Route::post('register', 'UserController@register'); |
|
| 35 | - Route::post('login', 'UserController@login'); |
|
| 36 | - Route::post('login/social', 'UserController@loginSocial'); |
|
| 37 | - Route::post('send/reset', 'UserController@sendReset'); |
|
| 38 | - Route::post('reset/password', 'UserController@resetPassword'); |
|
| 39 | - Route::post('change/password', 'UserController@changePassword'); |
|
| 40 | - Route::post('confirm/email', 'UserController@confirmEmail'); |
|
| 41 | - Route::post('resend/email/confirmation', 'UserController@resendEmailConfirmation'); |
|
| 42 | - }); |
|
| 30 | + Route::get('my', 'UserController@account'); |
|
| 31 | + Route::get('logout', 'UserController@logout'); |
|
| 32 | + Route::post('refresh/token', 'UserController@refreshToken'); |
|
| 33 | + Route::post('save', 'UserController@saveProfile'); |
|
| 34 | + Route::post('register', 'UserController@register'); |
|
| 35 | + Route::post('login', 'UserController@login'); |
|
| 36 | + Route::post('login/social', 'UserController@loginSocial'); |
|
| 37 | + Route::post('send/reset', 'UserController@sendReset'); |
|
| 38 | + Route::post('reset/password', 'UserController@resetPassword'); |
|
| 39 | + Route::post('change/password', 'UserController@changePassword'); |
|
| 40 | + Route::post('confirm/email', 'UserController@confirmEmail'); |
|
| 41 | + Route::post('resend/email/confirmation', 'UserController@resendEmailConfirmation'); |
|
| 42 | + }); |
|
| 43 | 43 | }); |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | | |
| 14 | 14 | */ |
| 15 | 15 | |
| 16 | -Route::group(['prefix' => 'users'], function () { |
|
| 16 | +Route::group(['prefix' => 'users'], function() { |
|
| 17 | 17 | |
| 18 | 18 | Route::get('/', 'UserController@index'); |
| 19 | 19 | Route::get('{id}', 'UserController@show'); |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | Route::patch('unblock/{id}', 'UserController@unblock'); |
| 26 | 26 | Route::patch('assign/roles/{id}', 'UserController@assignRoles'); |
| 27 | 27 | |
| 28 | - Route::group(['prefix' => 'account'], function () { |
|
| 28 | + Route::group(['prefix' => 'account'], function() { |
|
| 29 | 29 | |
| 30 | 30 | Route::get('my', 'UserController@account'); |
| 31 | 31 | Route::get('logout', 'UserController@logout'); |
@@ -8,52 +8,52 @@ |
||
| 8 | 8 | |
| 9 | 9 | class UserRepository extends BaseRepository |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * Init new object. |
|
| 13 | - * |
|
| 14 | - * @param AclUser $model |
|
| 15 | - * @return void |
|
| 16 | - */ |
|
| 17 | - public function __construct(AclUser $model) |
|
| 18 | - { |
|
| 19 | - parent::__construct($model); |
|
| 20 | - } |
|
| 11 | + /** |
|
| 12 | + * Init new object. |
|
| 13 | + * |
|
| 14 | + * @param AclUser $model |
|
| 15 | + * @return void |
|
| 16 | + */ |
|
| 17 | + public function __construct(AclUser $model) |
|
| 18 | + { |
|
| 19 | + parent::__construct($model); |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Detach all roles from the given user. |
|
| 24 | - * |
|
| 25 | - * @param mixed $user |
|
| 26 | - * @return object |
|
| 27 | - */ |
|
| 28 | - public function detachRoles($user) |
|
| 29 | - { |
|
| 30 | - $user = ! is_int($user) ? $user : $this->find($user); |
|
| 31 | - $user->roles()->detach(); |
|
| 32 | - } |
|
| 22 | + /** |
|
| 23 | + * Detach all roles from the given user. |
|
| 24 | + * |
|
| 25 | + * @param mixed $user |
|
| 26 | + * @return object |
|
| 27 | + */ |
|
| 28 | + public function detachRoles($user) |
|
| 29 | + { |
|
| 30 | + $user = ! is_int($user) ? $user : $this->find($user); |
|
| 31 | + $user->roles()->detach(); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Attach role ids to the given user. |
|
| 36 | - * |
|
| 37 | - * @param mixed $user |
|
| 38 | - * @param array $roleIds |
|
| 39 | - * @return object |
|
| 40 | - */ |
|
| 41 | - public function attachRoles($user, $roleIds) |
|
| 42 | - { |
|
| 43 | - $user = ! is_int($user) ? $user : $this->find($user); |
|
| 44 | - $user->roles()->attach($roleIds); |
|
| 45 | - } |
|
| 34 | + /** |
|
| 35 | + * Attach role ids to the given user. |
|
| 36 | + * |
|
| 37 | + * @param mixed $user |
|
| 38 | + * @param array $roleIds |
|
| 39 | + * @return object |
|
| 40 | + */ |
|
| 41 | + public function attachRoles($user, $roleIds) |
|
| 42 | + { |
|
| 43 | + $user = ! is_int($user) ? $user : $this->find($user); |
|
| 44 | + $user->roles()->attach($roleIds); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Count the given user the given roles. |
|
| 49 | - * |
|
| 50 | - * @param mixed $user |
|
| 51 | - * @param string[] $roles |
|
| 52 | - * @return boolean |
|
| 53 | - */ |
|
| 54 | - public function countRoles($user, $roles) |
|
| 55 | - { |
|
| 56 | - $user = ! is_int($user) ? $user : $this->find($user); |
|
| 57 | - return $user->roles()->whereIn('name', $roles)->count(); |
|
| 58 | - } |
|
| 47 | + /** |
|
| 48 | + * Count the given user the given roles. |
|
| 49 | + * |
|
| 50 | + * @param mixed $user |
|
| 51 | + * @param string[] $roles |
|
| 52 | + * @return boolean |
|
| 53 | + */ |
|
| 54 | + public function countRoles($user, $roles) |
|
| 55 | + { |
|
| 56 | + $user = ! is_int($user) ? $user : $this->find($user); |
|
| 57 | + return $user->roles()->whereIn('name', $roles)->count(); |
|
| 58 | + } |
|
| 59 | 59 | } |