Skip to content

Commit

Permalink
add method to get users along with their roles
Browse files Browse the repository at this point in the history
  • Loading branch information
uwla committed Jul 12, 2023
1 parent 7c54bfa commit 0981562
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
insert_final_newline = false
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
49 changes: 49 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# ignore vendor packages
node_modules
vendor

# ignore cache
__pycache__
.phpunit*

# ignore compiled assets
*.o
*.out
*.class

# ignore compressed files
*.tar
*.tar.bz2
*.tar.gz
*.tgz
*.bz2
*.rar
*.zip
*.7z

# ignore videos
*.mp4
*.mov
*.mkv
*.webm

# ignore R files
.RData
.Rproj.user
*.Rproj.user
*.Rhistory
*.Rproj
Rplots.pdf

# ignore LaTeX builds
*.log
*.fls
*.fdb_latexmk
*.aux
*.synctex.gz
_minted-*

# ignore other IDE files
tags
!tags/
.vscode
37 changes: 19 additions & 18 deletions src/Traits/HasPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ private function getRoleIds()
return UserRole::where('user_id', $id)->get()->pluck('role_id');
else if ($this instanceof Role)
return [$id];
else
throw new Exception('This class must be an instance of User or Role');
}


Expand Down Expand Up @@ -227,15 +229,15 @@ public function setPermissions($permissions)
/**
* get the permission ids associated with this object
*
* @return \Illuminate\Database\Eloquent\Collection<\Uwla\Lacl\Models\Permission>
* @return \Illuminate\Support\Collection
*/
private function getThisPermissionsIds()
{
$roleIds = $this->getRoleIds();

// if should have at least one role; otherwise has no permission
if (count($roleIds) == 0)
return [];
return collect([]);

// get the ids of the permissions associated with this object's roles
return RolePermission::query()
Expand All @@ -247,7 +249,7 @@ private function getThisPermissionsIds()
/**
* get all permissions associated with this object
*
* @return \Illuminate\Database\Eloquent\Collection<\Uwla\Lacl\Models\Permission>
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getPermissions()
{
Expand Down Expand Up @@ -315,7 +317,7 @@ public function getUserPermissions()
/**
* get the name of the permissions associated with this object
*
* @return array<string>
* @return \Illuminate\Support\Collection
*/
public function getPermissionNames()
{
Expand Down Expand Up @@ -435,7 +437,7 @@ public function countPermissions()
/**
* add single permission to many models
*
* @param \Uwla\Lacl\Permission|string $permission
* @param mixed $permission
* @param \Illuminate\Database\Eloquent\Collection $models
* @return void
*/
Expand All @@ -447,7 +449,7 @@ public static function addPermissionToMany($permission, $models)
/**
* add many permissions to many models
*
* @param \Uwla\Lacl\Permission[]|string[] $permission
* @param mixed $permission
* @param \Illuminate\Database\Eloquent\Collection $models
* @return void
*/
Expand Down Expand Up @@ -481,7 +483,7 @@ public static function addPermissionsToMany($permissions, $models)
/**
* delete a single permission from many models
*
* @param \Uwla\Lacl\Permission|string $permission
* @param mixed $permission
* @param \Illuminate\Database\Eloquent\Collection $models
* @return void
*/
Expand All @@ -493,7 +495,7 @@ public static function delPermissionFromMany($permission, $models)
/**
* delete many permissions from many models
*
* @param \Uwla\Lacl\Permission[]|string[] $permission
* @param mixed $permission
* @param \Illuminate\Database\Eloquent\Collection $models
* @return void
*/
Expand All @@ -520,9 +522,9 @@ public static function getIdColumn()
}

/**
* Get the given models with their permissions
* Get the given roles with their permissions
*
* @param \Illuminate\Database\Eloquent\Collection|array $models
* @param mixed $models
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function withPermissions($models)
Expand All @@ -534,12 +536,11 @@ public static function withPermissions($models)
$idCol = static::getIdColumn();

// get the model ids
$mids = $models->pluck();
$mids = $models->pluck($idCol);

// get the association models
$rps = RolePermission::query()
->whereIn('model_id', $mids)
->where('model', static::class)
->whereIn('role_id', $mids)
->get();

// get the permission ids
Expand All @@ -548,7 +549,7 @@ public static function withPermissions($models)
// get the permissions
$permissions = Permission::whereIn('id', $pids)->get();

// build a map ID -> MODEL
// build a map ID -> role
$id2model = [];
foreach($models as $m)
{
Expand Down Expand Up @@ -583,18 +584,18 @@ public static function withPermissions($models)
}

/**
* Get the given models with their permission names
* Get the given roles with their permission names
*
* @param \Illuminate\Database\Eloquent\Collection|array $models
* @param mixed $models
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function withPermissionNames($models)
{
$models = static::withPermission($models);
$models = static::withPermissions($models);
foreach ($models as $m)
$m->permissions = $m->permissions->pluck('name');
return $models;
}
}

?>
?>
Loading

0 comments on commit 0981562

Please sign in to comment.