Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/lyrasoft/luna
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Jan 9, 2024
2 parents b10fd08 + 8969c52 commit 2413a96
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion etc/social_login.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
'profile_handler' => \Lyrasoft\Luna\Auth\Profile\GoogleProfileHandler::class
],
'LINE' => [
'enabled' => true,
'enabled' => false,
'adapter' => \Lyrasoft\Luna\Auth\Provider\LineSocialProvider::class,
'keys' => [
'id' => env('LINE_SOCIAL_ID'),
Expand Down
31 changes: 25 additions & 6 deletions src/Access/AccessService.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function checkRoleAllowForRules(UserRole $role, array $rules): ?bool
*
* @return array<UserRole>
*/
public function getUserRoles(mixed $user): array
public function getUserRoles(mixed $user = null): array
{
$user = $this->getUser($user);

Expand Down Expand Up @@ -202,7 +202,7 @@ function () use ($id) {
*
* @return array<UserRoleMap>
*/
public function addRoleMapsToUser(mixed $user, mixed $roleMaps): array
public function addRoleMapsToUser(mixed $user = null, mixed $roleMaps = []): array
{
$currentRoleIds = collect($this->getUserRoles($user))
->map(fn(UserRole $map) => $map->getId())
Expand Down Expand Up @@ -243,7 +243,7 @@ public function addRoleMapsToUser(mixed $user, mixed $roleMaps): array
* @return array<UserRoleMap>
* @throws \ReflectionException
*/
public function addRolesToUser(mixed $user, mixed $roles, array $extra = []): array
public function addRolesToUser(mixed $user = null, mixed $roles = [], array $extra = []): array
{
$currentRoleIds = collect($this->getUserRoles($user))
->map(fn(UserRole $map) => $map->getId())
Expand Down Expand Up @@ -278,7 +278,7 @@ public function addRolesToUser(mixed $user, mixed $roles, array $extra = []): ar
return $maps;
}

public function removeRoleFromUser(mixed $user, mixed $roles): void
public function removeRoleFromUser(mixed $user = null, mixed $roles = []): void
{
$user = $this->getUser($user);

Expand All @@ -301,8 +301,9 @@ public function removeRoleFromUser(mixed $user, mixed $roles): void
);
}

public function userIsRole(mixed $user, string|int|UserRole $role): bool
public function userIsRole(mixed $user = null, mixed $role = null): bool
{
$user ??= $this->getUser();
$roleId = $this->unwrapRole($role);

$roles = $this->getUserRoles($user);
Expand All @@ -316,6 +317,23 @@ public function userIsRole(mixed $user, string|int|UserRole $role): bool
return false;
}

public function userInRoles(mixed $user = null, mixed $roles = []): bool
{
$user ??= $this->getUser();

if (!is_array($roles)) {
$roles = [$roles];
}

foreach ($roles as $role) {
if ($this->userIsRole($user, $role)) {
return true;
}
}

return false;
}

public function isSuperUser(mixed $user = null): bool
{
return $this->check(static::SUPERUSER_ACTION, $user);
Expand Down Expand Up @@ -394,8 +412,9 @@ public function isChildRole(UserRole|string|int $targetRole, UserRole|string|int
*
* @return array<UserRole>
*/
public function getAllowedRolesForUser(mixed $user): array
public function getAllowedRolesForUser(mixed $user = null): array
{
$user ??= $this->getUser();
$roles = $this->getRoles();
$userRoles = $this->getUserRoles($user);
$allowed = [];
Expand Down

0 comments on commit 2413a96

Please sign in to comment.