mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-29 18:20:01 +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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -22,6 +22,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Repositories\User;
|
||||
|
||||
use FireflyIII\Models\InvitedUser;
|
||||
use FireflyIII\Models\Role;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Collection;
|
||||
@@ -159,6 +160,30 @@ interface UserRepositoryInterface
|
||||
*/
|
||||
public function hasRole(User $user, string $role): bool;
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @param string $email
|
||||
* @return InvitedUser
|
||||
*/
|
||||
public function inviteUser(User $user, string $email): InvitedUser;
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function getInvitedUsers(): Collection;
|
||||
|
||||
/**
|
||||
* @param string $code
|
||||
* @return bool
|
||||
*/
|
||||
public function validateInviteCode(string $code): bool;
|
||||
|
||||
/**
|
||||
* @param string $code
|
||||
* @return void
|
||||
*/
|
||||
public function redeemCode(string $code): void;
|
||||
|
||||
/**
|
||||
* Remove any role the user has.
|
||||
*
|
||||
|
Reference in New Issue
Block a user