Skip to content

Commit

Permalink
chore: v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
frknasir committed Apr 9, 2022
1 parent 2e12233 commit 2b78a0d
Show file tree
Hide file tree
Showing 14 changed files with 397 additions and 62 deletions.
65 changes: 40 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
# Introduction

[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/support-ukraine.svg?t=1" />](https://supportukrainenow.org)

# A simple and straightforward Laravel user permissions package.

[![Latest Version on Packagist](https://img.shields.io/packagist/v/starfolksoftware/persona.svg?style=flat-square)](https://packagist.org/packages/starfolksoftware/persona)
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/starfolksoftware/persona/run-tests?label=tests)](https://github.com/starfolksoftware/persona/actions?query=workflow%3Arun-tests+branch%3Amain)
[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/starfolksoftware/persona/Check%20&%20fix%20styling?label=code%20style)](https://github.com/starfolksoftware/persona/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/starfolksoftware/persona.svg?style=flat-square)](https://packagist.org/packages/starfolksoftware/persona)

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.

## Support us

[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/persona.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/persona)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
A simple, elagant and straight-foward user permissions package for you Laravel applications.

## Installation

Expand All @@ -43,22 +27,53 @@ This is the contents of the published config file:

```php
return [
'roles' => [
// 'owner' => [
// 'key' => 'owner',
// 'name' => 'Owner',
// 'permissions' => ['*'],
// ],
],
];
```

Optionally, you can publish the views using
## Usage

```bash
php artisan vendor:publish --tag="persona-views"
Add the `HasRole` trait to your user model:

```php
use StarfolkSoftware\Persona\HasRole;

class User extends Authenticatable
{
use HasRole;

// ...
}
```

## Usage
Register your roles and permissions in the config file:

```php
return [
'roles' => [
'owner' => [
'key' => 'owner', // must be unique
'name' => 'Owner',
'permissions' => ['*'],
],
],
];
```

You can check if a user has a particular permission as in the following:

```php
$persona = new StarfolkSoftware\Persona();
echo $persona->echoPhrase('Hello, StarfolkSoftware!');
$user->hasPermission('post:edit');
```

Giving the `permissions` key the value `'*'` means the `hasPermission(...)` method will always return true.

## Testing

```bash
Expand All @@ -79,7 +94,7 @@ Please review [our security policy](../../security/policy) on how to report secu

## Credits

- [Faruk Nasir](https://github.com/starfolksoftware)
- [Faruk Nasir](https://github.com/frknasir)
- [All Contributors](../../contributors)

## License
Expand Down
8 changes: 7 additions & 1 deletion config/persona.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?php
// config for StarfolkSoftware/Persona
return [

'roles' => [
// 'owner' => [
// 'key' => 'owner',
// 'name' => 'Owner',
// 'permissions' => ['*'],
// ],
],
];
23 changes: 18 additions & 5 deletions database/migrations/create_persona_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,27 @@ use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('persona_table', function (Blueprint $table) {
$table->id();

// add fields
Schema::table('users', function (Blueprint $table) {
$table->string('role')->nullable();
});
}

$table->timestamps();
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('role');
});
}
};
Empty file removed resources/views/.gitkeep
Empty file.
19 changes: 0 additions & 19 deletions src/Commands/PersonaCommand.php

This file was deleted.

59 changes: 59 additions & 0 deletions src/HasRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace StarfolkSoftware\Persona;

use StarfolkSoftware\Persona\Role;
use Illuminate\Support\Str;
use StarfolkSoftware\Persona\Persona;

trait HasRole
{
/**
* Returns the user's role.
*
* @return \StarfolkSoftware\Persona\Role
*/
public function role(): Role
{
$role = $this->role;

return Persona::findRole($role);
}

/**
* Checks if the user has the given role.
*
* @param string $role
* @return bool
*/
public function hasRole(string $role): bool
{
return $this->role === $role;
}

/**
* Get the user's permissions.
*
* @return array
*/
public function permissions(): array
{
return (array) optional($this->role())->permissions;
}

/**
* Checks if the user has the given permission.
*
* @param string $permission
* @return bool
*/
public function hasPermission(string $permission): bool
{
$permissions = $this->permissions();

return in_array($permission, $permissions) ||
in_array('*', $permissions) ||
(Str::endsWith($permission, ':create') && in_array('*:create', $permissions)) ||
(Str::endsWith($permission, ':update') && in_array('*:update', $permissions));
}
}
91 changes: 90 additions & 1 deletion src/Persona.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,95 @@

namespace StarfolkSoftware\Persona;

class Persona
final class Persona
{
/**
* The roles that are available to assign to users.
*
* @var array
*/
public static $roles = [];

/**
* The permissions that exist within the application.
*
* @var array
*/
public static $permissions = [];

/**
* Checks if app has registered roles.
*
* @return bool
*/
public static function hasRoles(): bool
{
return ! empty(static::$roles);
}

/**
* Checks if app has registered permissions.
*
* @return bool
*/
public static function hasPermissions(): bool
{
return ! empty(static::$permissions);
}

/**
* Find the role with the given key.
*
* @param string $key
* @return \StarfolkSoftware\Persona\Role
*/
public static function findRole(string $key): Role
{
return static::$roles[$key] ?? null;
}

/**
* Define a role.
*
* @param string $key
* @param string $name
* @param array $permissions
* @return \StarfolkSoftware\Persona\Role
*/
public static function role(string $key, string $name, array $permissions): Role
{
static::$permissions = collect(array_merge(static::$permissions, $permissions))
->unique()
->sort()
->values()
->all();

return tap(new Role($key, $name, $permissions), function ($role) use ($key) {
static::$roles[$key] = $role;
});
}

/**
* Define the available permissions.
*
* @param array $permissions
* @return static
*/
public static function permissions(array $permissions)
{
static::$permissions = $permissions;

return new static;
}

/**
* Return the permissions in the given list that are actually defined permissions for the application.
*
* @param array $permissions
* @return array
*/
public static function validPermissions(array $permissions)
{
return array_values(array_intersect($permissions, static::$permissions));
}
}
23 changes: 20 additions & 3 deletions src/PersonaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use StarfolkSoftware\Persona\Commands\PersonaCommand;

class PersonaServiceProvider extends PackageServiceProvider
{
Expand All @@ -19,7 +18,25 @@ public function configurePackage(Package $package): void
->name('persona')
->hasConfigFile()
->hasViews()
->hasMigration('create_persona_table')
->hasCommand(PersonaCommand::class);
->hasMigration('create_persona_table');
}

public function packageBooted()
{
// register all roles
collect(config('persona.roles', []))->each(
fn ($role) => Persona::role(
$role['key'],
$role['name'],
$role['permissions'] ?? []
)
);

// register all permissions
$permissions = collect(config('persona.roles', []))->flatMap(
fn ($role) => $role['permissions'] ?? []
)->unique()->sort()->values()->all();

Persona::permissions($permissions);
}
}
Loading

0 comments on commit 2b78a0d

Please sign in to comment.