mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-31 02:36:28 +00:00 
			
		
		
		
	Add ability to invite users
This commit is contained in:
		| @@ -22,9 +22,11 @@ declare(strict_types=1); | ||||
|  | ||||
| namespace FireflyIII\Repositories\User; | ||||
|  | ||||
| use Carbon\Carbon; | ||||
| use Exception; | ||||
| use FireflyIII\Exceptions\FireflyException; | ||||
| use FireflyIII\Models\BudgetLimit; | ||||
| use FireflyIII\Models\InvitedUser; | ||||
| use FireflyIII\Models\Role; | ||||
| use FireflyIII\Models\UserGroup; | ||||
| use FireflyIII\User; | ||||
| @@ -103,22 +105,6 @@ class UserRepository implements UserRepositoryInterface | ||||
|         return true; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @return int | ||||
|      */ | ||||
|     public function count(): int | ||||
|     { | ||||
|         return $this->all()->count(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @return Collection | ||||
|      */ | ||||
|     public function all(): Collection | ||||
|     { | ||||
|         return User::orderBy('id', 'DESC')->get(['users.*']); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @param string $name | ||||
|      * @param string $displayName | ||||
| @@ -164,6 +150,22 @@ class UserRepository implements UserRepositoryInterface | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @return int | ||||
|      */ | ||||
|     public function count(): int | ||||
|     { | ||||
|         return $this->all()->count(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @return Collection | ||||
|      */ | ||||
|     public function all(): Collection | ||||
|     { | ||||
|         return User::orderBy('id', 'DESC')->get(['users.*']); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @param int $userId | ||||
|      * | ||||
| @@ -265,6 +267,24 @@ class UserRepository implements UserRepositoryInterface | ||||
|         return false; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @inheritDoc | ||||
|      */ | ||||
|     public function inviteUser(User $user, string $email): InvitedUser | ||||
|     { | ||||
|         $now = Carbon::now(); | ||||
|         $now->addDays(2); | ||||
|         $invitee = new InvitedUser; | ||||
|         $invitee->user()->associate($user); | ||||
|         $invitee->invite_code = Str::random(64); | ||||
|         $invitee->email       = $email; | ||||
|         $invitee->redeemed    = false; | ||||
|         $invitee->expires     = $now; | ||||
|         $invitee->save(); | ||||
|  | ||||
|         return $invitee; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Set MFA code. | ||||
|      * | ||||
| @@ -416,4 +436,34 @@ class UserRepository implements UserRepositoryInterface | ||||
|     { | ||||
|         return Role::where('name', $role)->first(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @inheritDoc | ||||
|      */ | ||||
|     public function getInvitedUsers(): Collection | ||||
|     { | ||||
|         return InvitedUser::with('user')->get(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @inheritDoc | ||||
|      */ | ||||
|     public function validateInviteCode(string $code): bool | ||||
|     { | ||||
|         $now     = Carbon::now(); | ||||
|         $invitee = InvitedUser::where('invite_code', $code)->where('expires', '<=', $now)->where('redeemed', 0)->first(); | ||||
|         return null !== $invitee; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @inheritDoc | ||||
|      */ | ||||
|     public function redeemCode(string $code): void | ||||
|     { | ||||
|         $obj = InvitedUser::where('invite_code', $code)->where('redeemed', 0)->first(); | ||||
|         if ($obj) { | ||||
|             $obj->redeemed = true; | ||||
|             $obj->save(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user