/app/Repositories/Backend/Auth/UserRepository.php
https://bitbucket.org/ghanu/chope · PHP · 362 lines · 204 code · 54 blank · 104 comment · 38 complexity · 84dc67b209cbb507bcd61aa4c10b3702 MD5 · raw file
- <?php
- namespace App\Repositories\Backend\Auth;
- use App\Models\Auth\User;
- use Illuminate\Support\Facades\DB;
- use App\Exceptions\GeneralException;
- use App\Repositories\BaseRepository;
- use Illuminate\Support\Facades\Hash;
- use App\Events\Backend\Auth\User\UserCreated;
- use App\Events\Backend\Auth\User\UserUpdated;
- use App\Events\Backend\Auth\User\UserRestored;
- use App\Events\Backend\Auth\User\UserConfirmed;
- use Illuminate\Pagination\LengthAwarePaginator;
- use App\Events\Backend\Auth\User\UserDeactivated;
- use App\Events\Backend\Auth\User\UserReactivated;
- use App\Events\Backend\Auth\User\UserUnconfirmed;
- use App\Events\Backend\Auth\User\UserPasswordChanged;
- use App\Notifications\Backend\Auth\UserAccountActive;
- use App\Events\Backend\Auth\User\UserPermanentlyDeleted;
- use App\Notifications\Frontend\Auth\UserNeedsConfirmation;
- /**
- * Class UserRepository.
- */
- class UserRepository extends BaseRepository
- {
- /**
- * @return string
- */
- public function model()
- {
- return User::class;
- }
- /**
- * @return mixed
- */
- public function getUnconfirmedCount() : int
- {
- return $this->model
- ->where('confirmed', 0)
- ->count();
- }
- /**
- * @param int $paged
- * @param string $orderBy
- * @param string $sort
- *
- * @return mixed
- */
- public function getActivePaginated($paged = 25, $orderBy = 'created_at', $sort = 'desc') : LengthAwarePaginator
- {
- return $this->model
- ->with('roles', 'permissions', 'providers')
- ->active()
- ->orderBy($orderBy, $sort)
- ->paginate($paged);
- }
- /**
- * @param int $paged
- * @param string $orderBy
- * @param string $sort
- *
- * @return LengthAwarePaginator
- */
- public function getInactivePaginated($paged = 25, $orderBy = 'created_at', $sort = 'desc') : LengthAwarePaginator
- {
- return $this->model
- ->with('roles', 'permissions', 'providers')
- ->active(false)
- ->orderBy($orderBy, $sort)
- ->paginate($paged);
- }
- /**
- * @param int $paged
- * @param string $orderBy
- * @param string $sort
- *
- * @return LengthAwarePaginator
- */
- public function getDeletedPaginated($paged = 25, $orderBy = 'created_at', $sort = 'desc') : LengthAwarePaginator
- {
- return $this->model
- ->with('roles', 'permissions', 'providers')
- ->onlyTrashed()
- ->orderBy($orderBy, $sort)
- ->paginate($paged);
- }
- /**
- * @param array $data
- *
- * @return User
- * @throws \Exception
- * @throws \Throwable
- */
- public function create(array $data) : User
- {
- return DB::transaction(function () use ($data) {
- $user = parent::create([
- 'first_name' => $data['first_name'],
- 'last_name' => $data['last_name'],
- 'email' => $data['email'],
- 'timezone' => $data['timezone'],
- 'password' => Hash::make($data['password']),
- 'active' => isset($data['active']) && $data['active'] == '1' ? 1 : 0,
- 'confirmation_code' => md5(uniqid(mt_rand(), true)),
- 'confirmed' => isset($data['confirmed']) && $data['confirmed'] == '1' ? 1 : 0,
- ]);
- // See if adding any additional permissions
- if (! isset($data['permissions']) || ! count($data['permissions'])) {
- $data['permissions'] = [];
- }
- if ($user) {
- // User must have at least one role
- if (! count($data['roles'])) {
- throw new GeneralException(__('exceptions.backend.access.users.role_needed_create'));
- }
- // Add selected roles/permissions
- $user->syncRoles($data['roles']);
- $user->syncPermissions($data['permissions']);
- //Send confirmation email if requested and account approval is off
- if (isset($data['confirmation_email']) && $user->confirmed == 0 && ! config('access.users.requires_approval')) {
- $user->notify(new UserNeedsConfirmation($user->confirmation_code));
- }
- event(new UserCreated($user));
- return $user;
- }
- throw new GeneralException(__('exceptions.backend.access.users.create_error'));
- });
- }
- /**
- * @param User $user
- * @param array $data
- *
- * @return User
- * @throws GeneralException
- * @throws \Exception
- * @throws \Throwable
- */
- public function update(User $user, array $data) : User
- {
- $this->checkUserByEmail($user, $data['email']);
- // See if adding any additional permissions
- if (! isset($data['permissions']) || ! count($data['permissions'])) {
- $data['permissions'] = [];
- }
- return DB::transaction(function () use ($user, $data) {
- if ($user->update([
- 'first_name' => $data['first_name'],
- 'last_name' => $data['last_name'],
- 'email' => $data['email'],
- 'timezone' => $data['timezone'],
- ])) {
- // Add selected roles/permissions
- $user->syncRoles($data['roles']);
- $user->syncPermissions($data['permissions']);
- event(new UserUpdated($user));
- return $user;
- }
- throw new GeneralException(__('exceptions.backend.access.users.update_error'));
- });
- }
- /**
- * @param User $user
- * @param $input
- *
- * @return User
- * @throws GeneralException
- */
- public function updatePassword(User $user, $input) : User
- {
- $user->password = Hash::make($input['password']);
- if ($user->save()) {
- event(new UserPasswordChanged($user));
- return $user;
- }
- throw new GeneralException(__('exceptions.backend.access.users.update_password_error'));
- }
- /**
- * @param User $user
- * @param $status
- *
- * @return User
- * @throws GeneralException
- */
- public function mark(User $user, $status) : User
- {
- if (auth()->id() == $user->id && $status == 0) {
- throw new GeneralException(__('exceptions.backend.access.users.cant_deactivate_self'));
- }
- $user->active = $status;
- switch ($status) {
- case 0:
- event(new UserDeactivated($user));
- break;
- case 1:
- event(new UserReactivated($user));
- break;
- }
- if ($user->save()) {
- return $user;
- }
- throw new GeneralException(__('exceptions.backend.access.users.mark_error'));
- }
- /**
- * @param User $user
- *
- * @return User
- * @throws GeneralException
- */
- public function confirm(User $user) : User
- {
- if ($user->confirmed) {
- throw new GeneralException(__('exceptions.backend.access.users.already_confirmed'));
- }
- $user->confirmed = 1;
- $confirmed = $user->save();
- if ($confirmed) {
- event(new UserConfirmed($user));
- // Let user know their account was approved
- if (config('access.users.requires_approval')) {
- $user->notify(new UserAccountActive);
- }
- return $user;
- }
- throw new GeneralException(__('exceptions.backend.access.users.cant_confirm'));
- }
- /**
- * @param User $user
- *
- * @return User
- * @throws GeneralException
- */
- public function unconfirm(User $user) : User
- {
- if (! $user->confirmed) {
- throw new GeneralException(__('exceptions.backend.access.users.not_confirmed'));
- }
- if ($user->id == 1) {
- // Cant un-confirm admin
- throw new GeneralException(__('exceptions.backend.access.users.cant_unconfirm_admin'));
- }
- if ($user->id == auth()->id()) {
- // Cant un-confirm self
- throw new GeneralException(__('exceptions.backend.access.users.cant_unconfirm_self'));
- }
- $user->confirmed = 0;
- $unconfirmed = $user->save();
- if ($unconfirmed) {
- event(new UserUnconfirmed($user));
- return $user;
- }
- throw new GeneralException(__('exceptions.backend.access.users.cant_unconfirm'));
- }
- /**
- * @param User $user
- *
- * @return User
- * @throws GeneralException
- * @throws \Exception
- * @throws \Throwable
- */
- public function forceDelete(User $user) : User
- {
- if (is_null($user->deleted_at)) {
- throw new GeneralException(__('exceptions.backend.access.users.delete_first'));
- }
- return DB::transaction(function () use ($user) {
- // Delete associated relationships
- $user->providers()->delete();
- if ($user->forceDelete()) {
- event(new UserPermanentlyDeleted($user));
- return $user;
- }
- throw new GeneralException(__('exceptions.backend.access.users.delete_error'));
- });
- }
- /**
- * @param User $user
- *
- * @return User
- * @throws GeneralException
- */
- public function restore(User $user) : User
- {
- if (is_null($user->deleted_at)) {
- throw new GeneralException(__('exceptions.backend.access.users.cant_restore'));
- }
- if ($user->restore()) {
- event(new UserRestored($user));
- return $user;
- }
- throw new GeneralException(__('exceptions.backend.access.users.restore_error'));
- }
- /**
- * @param User $user
- * @param $email
- *
- * @throws GeneralException
- */
- protected function checkUserByEmail(User $user, $email)
- {
- //Figure out if email is not the same
- if ($user->email != $email) {
- //Check to see if email exists
- if ($this->model->where('email', '=', $email)->first()) {
- throw new GeneralException(trans('exceptions.backend.access.users.email_error'));
- }
- }
- }
- }