Skip to content

Commit

Permalink
feat: added Lar9 support
Browse files Browse the repository at this point in the history
  • Loading branch information
syntaxlexx committed Apr 20, 2022
2 parents 670b419 + 88e5ec0 commit f485472
Show file tree
Hide file tree
Showing 50 changed files with 1,655 additions and 95 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [7.4, 7.3, 7.2, 8.0]
php: [8.0]
stability: [prefer-lowest, prefer-stable]

name: PHP ${{ matrix.php }} - ${{ matrix.stability }}
Expand Down
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@ This fork is meant to be used on AceLords projects, and thus contain other funct
# Laratrust (Laravel Package)

[![tests](https://github.com/santigarcor/laratrust/workflows/tests/badge.svg)](https://github.com/santigarcor/laratrust/actions?query=workflow%3Atests)
[![Latest Stable Version](https://poser.pugx.org/santigarcor/laratrust/v)](//packagist.org/packages/santigarcor/laratrust)
[![Total Downloads](https://poser.pugx.org/santigarcor/laratrust/downloads)](//packagist.org/packages/santigarcor/laratrust)
[![StyleCI](https://styleci.io/repos/59691202/shield)](https://styleci.io/repos/59691202)
[![License](https://poser.pugx.org/santigarcor/laratrust/license)](//packagist.org/packages/santigarcor/laratrust)
[![Latest Stable Version](https://poser.pugx.org/acelords/laratrust/v)](//packagist.org/packages/acelords/laratrust)
[![Total Downloads](https://poser.pugx.org/acelords/laratrust/downloads)](//packagist.org/packages/acelords/laratrust)
[![License](https://poser.pugx.org/acelords/laratrust/license)](//packagist.org/packages/acelords/laratrust)

## Version Compatibility

Laravel | Laratrust
:---------|:----------
9.x | [^6.3.1.3](https://laratrust.santigarcor.me/docs/6.x/)
8.x | [6.x](https://laratrust.santigarcor.me/docs/6.x/)
9.x | [7.x](https://laratrust.santigarcor.me/docs/7.x/)
8.x | [6.x](https://laratrust.santigarcor.me/docs/6.x/)
7.x | [6.x](https://laratrust.santigarcor.me/docs/6.x/)
6.x | [6.x](https://laratrust.santigarcor.me/docs/6.x/)
5.6.x - 5.8.x | [5.2](https://laratrust.santigarcor.me/docs/5.2/)
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
],
"require": {
"php": ">=7.2",
"kkszymanowski/traitor": "^0.2.0",
"laravel/framework": "~6.0|~7.0|~8.0|~9.0"
"kkszymanowski/traitor": "^1.0",
"laravel/framework": "~6.0|~7.0|~8.0|^9.0"
},
"require-dev": {
"mockery/mockery": "^1.3.2",
"orchestra/testbench": "4.*|5.*|6.*",
"orchestra/testbench": "4.*|5.*|6.*|7.*",
"phpunit/phpunit": "^7.5.15|^8.4|^9.0"
},
"autoload": {
Expand Down
22 changes: 21 additions & 1 deletion config/laratrust.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
| NOTE: Currently the database check does not use cache.
|
*/
'enabled' => env('LARATRUST_ENABLE_CACHE', true),
'enabled' => env('LARATRUST_ENABLE_CACHE', env('APP_ENV') === 'production'),

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -240,6 +240,16 @@
*/
'magic_is_able_to_method_case' => 'kebab_case',

/*
|--------------------------------------------------------------------------
| Laratrust Permissions as Gates
|--------------------------------------------------------------------------
|
| Determines if you can check if a user has a permission using the "can" method.
|
*/
'permissions_as_gates' => false,

/*
|--------------------------------------------------------------------------
| Laratrust Panel
Expand Down Expand Up @@ -301,6 +311,16 @@
*/
'assign_permissions_to_user' => true,

/*
|--------------------------------------------------------------------------
| Enable permissions creation
|--------------------------------------------------------------------------
|
| Enable/Disable the possibility to create permissions from the panel.
|
*/
'create_permissions' => true,

/*
|--------------------------------------------------------------------------
| Add restriction to roles in the panel
Expand Down
1 change: 1 addition & 0 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module.exports = {
'/docs/5.1/': getDocsNavBar('5.1'),
'/docs/5.2/': getDocsNavBar('5.2'),
'/docs/6.x/': getDocsNavBar('6.x'),
'/docs/7.x/': getDocsNavBar('7.x'),
},
nav: [
{ text: 'Docs', link: getActiveVersion().link },
Expand Down
6 changes: 3 additions & 3 deletions docs/.vuepress/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const ACTIVE_VERSION = '6.x';
const ACTIVE_VERSION = '7.x';

function getDocsNavBar(version) {
switch (version) {
Expand Down Expand Up @@ -36,7 +36,7 @@ function getDocsNavBar(version) {
'license',
'contributing',
];
break;
case '7.x':
case '6.x':
return [
'upgrade',
Expand Down Expand Up @@ -73,12 +73,12 @@ function getDocsNavBar(version) {
'license',
'contributing',
];
break;
}
}

function getVersionsLinks(preLink = 'docs') {
let links = [
{ text: '7.x', link: `/${preLink}/7.x/` },
{ text: '6.x', link: `/${preLink}/6.x/` },
{ text: '5.2', link: `/${preLink}/5.2/` },
{ text: '5.1', link: `/${preLink}/5.1/` },
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Laratrust is an easy and flexible way to add roles, permissions and teams author

Laravel | Laratrust
:---------|:----------
9.x | [7.x](/docs/7.x/)
8.x | [6.x](/docs/6.x/)
7.x | [6.x](/docs/6.x/)
6.x | [6.x](/docs/6.x/)
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/6.x/usage/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class AppServiceProvider extends ServiceProvider

::: tip NOTE
- Inside your observable classes you can have your normal model events methods alongside Laratrust's events methods.
- If you wan to register Laratrust events and also eloquent events yo should call both `observe` and `laratrustObserve` methods.
- If you want to register Laratrust events and also eloquent events you should call both `observe` and `laratrustObserve` methods.
:::

### Flushing events and observables
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/6.x/usage/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ If you are using the teams feature and want to use the middleware checking for y
// $user->hasRole(['admin', 'root'], 'my-awesome-team', true);

'middleware' => ['permission:edit-post|edit-user,my-awesome-team,require_all']
// $user->hasRole(['edit-post', 'edit-user'], 'my-awesome-team', true);
// $user->isAbleTo(['edit-post', 'edit-user'], 'my-awesome-team', true);

'middleware' => ['ability:admin|owner,create-post|edit-user,my-awesome-team,require_all']
// $user->ability(['admin', 'owner'], ['create-post', 'edit-user'], 'my-awesome-team', true);
Expand Down
7 changes: 4 additions & 3 deletions docs/docs/6.x/usage/teams.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ The user ability is the same, but this time you can pass the team parameter.

```php
$options = [
'requireAll' => true, //Default: false,
'foreignKeyName' => 'canBeAnyString' //Default: null
'validate_all' => true, //Default: false
'return_type' => 'array' //Default: 'boolean'. You can also set it as 'both'
];

$user->ability(['admin'], ['edit-user'], 'my-awesome-team');
Expand All @@ -130,6 +130,7 @@ $user->ability(['admin'], ['edit-user'], 'my-awesome-team', $options);
## Permissions, Roles & Ownership Checks
The permissions, roles and ownership checks work the same, but this time you can pass the team in the options array.


```php
$options = [
'team' => 'my-awesome-team',
Expand All @@ -140,4 +141,4 @@ $options = [
$post = Post::find(1);
$user->canAndOwns(['edit-post', 'delete-post'], $post, $options);
$user->hasRoleAndOwns(['admin', 'writer'], $post, $options);
```
```
31 changes: 31 additions & 0 deletions docs/docs/7.x/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Introduction

Laratrust is a Laravel package that lets you handle very easily roles and permissions inside your application. All of this through a very simple configuration process and API.

Here you can see some examples:

```php
$adminRole = Role::where('name', 'admin')->first();
$editUserPermission = Permission::where('name', 'edit-user')->first();
$user = User::find(1);

$user->attachRole($adminRole);
// Or
$user->attachRole('admin');

$user->attachPermission($editUserPermission);
// Or
$user->attachPermission('edit-user');
```

You can also check if a user has some permissions or roles:

```php
$user->isAbleTo('edit-user');

$user->hasRole('admin');
$user->isA('guide');
$user->isAn('admin');
```

It also supports teams, multiple users, objects ownerships, it has a simple admin panel and it is compatible with Laravel's policies and gates system.
7 changes: 7 additions & 0 deletions docs/docs/7.x/contributing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Contributing

We use StyleCI so there is no problem with the styling in your pull requests.

Please report any issue you find in the package or in the docs.

Pull requests are welcome.
44 changes: 44 additions & 0 deletions docs/docs/7.x/installation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Installation

1. You can install the package using composer:
```bash
composer require santigarcor/laratrust
```

2. Publish the configuration file:

```bash
php artisan vendor:publish --tag="laratrust"
```
::: warning
If this command did not publish any files, chances are, the Laratrust service provider hasn't been registered. Try clearing your configuration cache
```bash
php artisan config:clear
```
:::

3. Run the setup command:

::: tip IMPORTANT
**Before running the command go to your* `config/laratrust.php` *file and change the values according to your needs.**
:::

```bash
php artisan laratrust:setup
```

This command will generate the migrations, create the `Role` and `Permission` models (if you are using the teams feature it will also create a `Team` model) and will add the trait to the configured user models.

4. Dump the autoloader:
```bash
composer dump-autoload
```

5. Run the migrations:
```bash
php artisan migrate
```

::: tip IMPORTANT
**If you did the steps above you are done with the configuration, if not, please read and follow the whole configuration process**
:::
3 changes: 3 additions & 0 deletions docs/docs/7.x/license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# License

Laratrust is free software distributed under the terms of the MIT license.
10 changes: 10 additions & 0 deletions docs/docs/7.x/the-basics/migrations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Migrations

The migration will create five (or six if you use teams feature) tables in your database:

* `roles` — stores role records.
* `permissions` — stores permission records.
* `teams` — stores teams records (Only if you use the teams feature).
* `role_user` — stores [polymorphic](https://laravel.com/docs/eloquent-relationships#polymorphic-relations) relations between roles and users.
* `permission_role` — stores [many-to-many](https://laravel.com/docs/eloquent-relationships#many-to-many) relations between roles and permissions.
* `permission_user` — stores [polymorphic](https://laravel.com/docs/eloquent-relationships#polymorphic-relations) relations between users and permissions.
21 changes: 21 additions & 0 deletions docs/docs/7.x/the-basics/models/permission.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Permission

```php
<?php

namespace App;

use Laratrust\Models\LaratrustPermission;

class Permission extends LaratrustPermission
{
}
```

The `Permission` model has the same three attributes as the `Role`:

* `name` — Unique name for the permission, used for looking up permission information in the application layer. For example: "create-post", "edit-user", "post-payment", "mailing-list-subscribe".
* `display_name` — Human readable name for the permission. Not necessarily unique and optional. For example "Create Posts", "Edit Users", "Post Payments", "Subscribe to mailing list".
* `description` — A more detailed explanation of the Permission.

In general, it may be helpful to think of the last two attributes in the form of a sentence: "The permission `display_name` allows a user to `description`."
21 changes: 21 additions & 0 deletions docs/docs/7.x/the-basics/models/role.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Role

```php
<?php

namespace App;

use Laratrust\Models\LaratrustRole;

class Role extends LaratrustRole
{
}
```

The `Role` model has three main attributes:

* `name` — Unique name for the Role, used for looking up role information in the application layer. For example: "admin", "owner", "employee".
* `display_name` — Human readable name for the Role. Not necessarily unique and optional. For example: "User Administrator", "Project Owner", "Widget Co. Employee".
* `description` — A more detailed explanation of what the Role does. Also, optional.

Both `display_name` and `description` are optional; their fields are nullable in the database.
26 changes: 26 additions & 0 deletions docs/docs/7.x/the-basics/models/team.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Team

::: tip IMPORTANT
Only applies if you are using the teams feature.
:::

```php
<?php

namespace App;

use Laratrust\Models\LaratrustTeam;

class Team extends LaratrustTeam
{
}
```

The `Team` model has three main attributes:

* `name` — Unique name for the Team, used for looking up team information in the application layer. For example: "my-team", "my-company".
* `display_name` — Human readable name for the Team. Not necessarily unique and optional. For example: "My Team", "My Company".
* `description` — A more detailed explanation of what the Team does. Also, optional.

Both `display_name` and `description` are optional; their fields are nullable in the database.

16 changes: 16 additions & 0 deletions docs/docs/7.x/the-basics/models/user.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# User

```php
<?php

use Laratrust\Traits\LaratrustUserTrait;

class User extends Model
{
use LaratrustUserTrait; // add this trait to your user model

...
}
```

This class uses the `LaratrustUserTrait` to enable the relationships with `Role` and `Permission`.It also adds the following methods `roles()`, `hasRole($name)`, `hasPermission($permission)`, `isAbleTo($permission)`, `ability($roles, $permissions, $options)`, and `rolesTeams()` to the model.
24 changes: 24 additions & 0 deletions docs/docs/7.x/the-basics/teams.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Teams Feature

::: tip NOTE
The teams feature is **optional**, this part covers how to configure it after the installation.

If you had your `teams.enabled` value set to `true` during the installation and automatic setup, you can skip this part.
:::


1. Set the `teams.enabled` value to `true` in your `config/laratrust.php` file.

2. Run:
```bash
php artisan laratrust:setup-teams
```

3. Run:
```bash
php artisan migrate
```

Now you can use the teams feature in you code.

Please refer to the <docs-link to="/usage/teams.html">teams concepts</docs-link> concepts to learn how to use them.
Loading

0 comments on commit f485472

Please sign in to comment.