Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
iWirk committed Nov 5, 2020
0 parents commit 0980a7f
Show file tree
Hide file tree
Showing 15 changed files with 418 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/idea
1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
preset: laravel
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changelog

All notable changes to `Cerberus` will be documented in this file.

## Version 1.0

### Added
- Everything
41 changes: 41 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "emodyz/cerberus",
"description": "Hell's Gatekeeper",
"license": "GPL-3.0-only",
"authors": [
{
"name": "Saber Nouira",
"email": "contact@emodyz.eu",
"homepage": "https://emodyz.eu"
}
],
"homepage": "https://github.com/emodyz/cerberus",
"keywords": ["Laravel", "Cerberus"],
"require": {
"illuminate/support": "~7|~8"
},
"require-dev": {
"phpunit/phpunit": "~9.0",
"orchestra/testbench": "~5|~6"
},
"autoload": {
"psr-4": {
"Emodyz\\Cerberus\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Emodyz\\Cerberus\\Tests\\": "tests"
}
},
"extra": {
"laravel": {
"providers": [
"Emodyz\\Cerberus\\CerberusServiceProvider"
],
"aliases": {
"Cerberus": "Emodyz\\Cerberus\\Facades\\Cerberus"
}
}
}
}
26 changes: 26 additions & 0 deletions config/cerberus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

return [
'roles' => [
'owner' => [
'displayName' => 'Owner',
'can' => [
'*'
]
],
'admin' => [
'displayName' => 'Administrator',
'can' => []
],
'default' => [
'displayName' => 'User',
'can' => []
]
],
'authorizations' => [
[
'slug' => '*',
'description' => 'a user can do everything'
],
]
];
27 changes: 27 additions & 0 deletions contributing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Contributing

Contributions are welcome and will be fully credited.

Contributions are accepted via Pull Requests on [Github](https://github.com/emodyz/cerberus).

# Things you could do
If you want to contribute but do not know where to start, this list provides some starting points.
- Add license text
- Remove rewriteRules.php
- Set up TravisCI, StyleCI, ScrutinizerCI
- Write a comprehensive ReadMe

## Pull Requests

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

- **Document any change in behaviour** - Make sure the `readme.md` and any other relevant documentation are kept up-to-date.

- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.


**Happy coding**!
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddRoleColumnToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('role')->default('default')->index();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
//
});
}
}
5 changes: 5 additions & 0 deletions license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# The license

Copyright (c) author name <author email>

...Add your license text here...
22 changes: 22 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Package">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src/</directory>
</whitelist>
</filter>
</phpunit>
57 changes: 57 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Cerberus

[![Latest Version on Packagist][ico-version]][link-packagist]
[![Total Downloads][ico-downloads]][link-downloads]
[![Build Status][ico-travis]][link-travis]
[![StyleCI][ico-styleci]][link-styleci]

This is where your description should go. Take a look at [contributing.md](contributing.md) to see a to do list.

## Installation

Via Composer

``` bash
$ composer require emodyz/cerberus
```

## Usage

## Change log

Please see the [changelog](changelog.md) for more information on what has changed recently.

## Testing

``` bash
$ composer test
```

## Contributing

Please see [contributing.md](contributing.md) for details and a todolist.

## Security

If you discover any security related issues, please email author email instead of using the issue tracker.

## Credits

- [author name][link-author]
- [All Contributors][link-contributors]

## License

license. Please see the [license file](license.md) for more information.

[ico-version]: https://img.shields.io/packagist/v/emodyz/cerberus.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/emodyz/cerberus.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/emodyz/cerberus/master.svg?style=flat-square
[ico-styleci]: https://styleci.io/repos/12345678/shield

[link-packagist]: https://packagist.org/packages/emodyz/cerberus
[link-downloads]: https://packagist.org/packages/emodyz/cerberus
[link-travis]: https://travis-ci.org/emodyz/cerberus
[link-styleci]: https://styleci.io/repos/12345678
[link-author]: https://github.com/emodyz
[link-contributors]: ../../contributors
37 changes: 37 additions & 0 deletions src/AuthorizationRegistrar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Emodyz\Cerberus;

use Illuminate\Contracts\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Access\Gate;

class AuthorizationRegistrar
{

// TODO: cache everything

/**
* PermissionManager constructor.
*/
public function __construct()
{
//
}

/**
* Register the permission check method on the gate.
* We resolve the Gate fresh here, for benefit of long-running instances.
*
* @return bool
*/
public function registerPermissions(): bool
{
app(Gate::class)->before(function (Authorizable $user, string $ability) {
if (method_exists($user, 'checkAuthorizationTo')) {
return $user->checkAuthorizationTo($ability) ?: null;
}
});

return true;
}
}
20 changes: 20 additions & 0 deletions src/Cerberus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Emodyz\Cerberus;

class Cerberus
{
public static function config($key = null): \Illuminate\Support\Collection
{
switch ($key) {
case 'roles':
return collect(config('cerberus.roles'));
case 'authorizations':
return collect(config('cerberus.authorizations'));
case !null:
return collect(config('cerberus.' . $key));
default:
return collect(config('cerberus'));
}
}
}
96 changes: 96 additions & 0 deletions src/CerberusServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Emodyz\Cerberus;

use Illuminate\Support\ServiceProvider;

class CerberusServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
*
* @param AuthorizationRegistrar $permissionLoader
* @return void
*/
public function boot(AuthorizationRegistrar $permissionLoader): void
{
// $this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'emodyz');
// $this->loadViewsFrom(__DIR__.'/../resources/views', 'emodyz');
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
// $this->loadRoutesFrom(__DIR__.'/routes.php');

// Publishing is only necessary when using the CLI.
if ($this->app->runningInConsole()) {
$this->bootForConsole();
}

$permissionLoader->registerPermissions();

$this->app->singleton(AuthorizationRegistrar::class, function ($app) use ($permissionLoader) {
return $permissionLoader;
});
}

/**
* Register any package services.
*
* @return void
*/
public function register(): void
{
$this->mergeConfigFrom(__DIR__.'/../config/cerberus.php', 'cerberus');

// Register the service the package provides.
$this->app->singleton('cerberus', function ($app) {
return new Cerberus();
});

// $this->app->alias(Cerberus::class, 'cerberus');
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['cerberus'];
}

/**
* Console-specific booting.
*
* @return void
*/
protected function bootForConsole(): void
{
// Publishing the configuration file.
$this->publishes([
__DIR__.'/../config/cerberus.php' => config_path('cerberus.php'),
], 'cerberus.config');

// Publishing the migration files.
$this->publishes([
__DIR__.'/../database/migrations' => base_path('database/migrations/emodyz/cerberus'),
], 'cerberus.migrations');

// Publishing the views.
/*$this->publishes([
__DIR__.'/../resources/views' => base_path('resources/views/vendor/emodyz/cerberus'),
], 'cerberus.views');*/

// Publishing assets.
/*$this->publishes([
__DIR__.'/../resources/assets' => public_path('vendor/emodyz/cerberus'),
], 'cerberus.views');*/

// Publishing the translation files.
/*$this->publishes([
__DIR__.'/../resources/lang' => resource_path('lang/vendor/emodyz/cerberus'),
], 'cerberus.views');*/

// Registering package commands.
// $this->commands([]);
}
}
Loading

0 comments on commit 0980a7f

Please sign in to comment.