permissions

This commit is contained in:
2023-10-17 22:57:25 +03:00
parent 4b9e9cb4f6
commit 632684f514
29 changed files with 370 additions and 223 deletions
+27 -14
View File
@@ -25,14 +25,19 @@ readonly class AuthService
public function currentUserId(): ?string
{
if(app()->runningInConsole()){
if (app()->runningInConsole()) {
return config("lucent.systemUserId");
}else{
} else {
return $this->session->get("user.id");
}
}
public function currentUserRoles(): array
{
return $this->session->get("user.roles") ?? [];
}
public function isLoggedIn(): bool
{
return !empty($this->currentUserId());
@@ -50,7 +55,7 @@ readonly class AuthService
throw new LucentException("You account was not found");
}
if ($user->get()->role === Role::REMOVED) {
if ($user->get()->isRemoved()) {
throw new LucentException("Your account is not active");
}
@@ -66,18 +71,17 @@ readonly class AuthService
$newUser->updatedAt = Carbon::now()->toJson();
$newUser->mailToken = null;
$this->userRepo->update($newUser);
$this->session->put(["user" => $user->get()->safe()]);
}
public function create(string $name, string $email, string $role): User
public function create(string $name, string $email, array $roles): User
{
$user = new User(
id: (string)Str::uuid(),
name: new Name($name),
email: new Email($email),
role: Role::from($role),
roles: $this->validateRoles($roles),
createdAt: Carbon::now()->toJson(),
updatedAt: Carbon::now()->toJson(),
loggedInAt: Carbon::now()->toJson(),
@@ -101,7 +105,7 @@ readonly class AuthService
throw new LucentException("User not found");
}
if ($user->get()->role === Role::REMOVED) {
if ($user->get()->isRemoved()) {
throw new LucentException("Cannot reset email if the user is not active");
}
@@ -121,7 +125,7 @@ readonly class AuthService
/**
* @throws LucentException
*/
public function changeRole(string $userId, string $newRole): void
public function changeRoles(string $userId, array $roles): void
{
$user = $this->userRepo->findById($userId);
@@ -130,7 +134,7 @@ readonly class AuthService
}
$newUser = $user->get();
$newUser->role = Role::from($newRole);
$newUser->roles = $this->validateRoles($roles);
$newUser->updatedAt = Carbon::now()->toJson();
$this->userRepo->update($newUser);
}
@@ -138,7 +142,7 @@ readonly class AuthService
/**
* @throws LucentException
*/
public function updateName( string $name): void
public function updateName(string $name): void
{
$name = (new Name($name));
$this->userRepo->updateName($this->currentUserId(), $name);
@@ -153,7 +157,7 @@ readonly class AuthService
{
$email = (new Email($email));
$user = $this->userRepo->findByEmail($email);
if($user->isDefined()){
if ($user->isDefined()) {
throw new LucentException("Email already assigned to user");
}
@@ -169,10 +173,10 @@ readonly class AuthService
public function invite(
string $name,
string $email,
string $role
array $roles
): User
{
$user = $this->create($name, $email, $role);
$user = $this->create($name, $email, $roles);
$this->sendLoginEmail($user->email);
return $user;
}
@@ -185,10 +189,19 @@ readonly class AuthService
string $email
): User
{
$user = $this->invite($name, $email, "admin");
$user = $this->invite($name, $email, ["admin"]);
$this->sendLoginEmail($user->email);
return $user;
}
public function validateRoles(array $roles): array
{
return collect($roles)
->filter(fn(string $role) => in_array($role, $this->channelService->channel->roles))
->unique()
->values()
->toArray();
}
}