All Projects → vantageoy → authorized-attributes

vantageoy / authorized-attributes

Licence: MIT License
Authorized Model Attributes for Laravel

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to authorized-attributes

Bouncer
Eloquent roles and abilities.
Stars: ✭ 2,763 (+12459.09%)
Mutual labels:  eloquent, authorization
dictator
Dictates what your users see. Plug-based authorization.
Stars: ✭ 77 (+250%)
Mutual labels:  authorization
security-wrapper
对springSecurity进行二次开发,提供OAuth2授权(支持跨域名,多应用授权)、JWT、SSO、文件上传、权限系统无障碍接入、接口防刷、XSS、CSRF、SQL注入、三方登录(绑定,解绑)、加密通信等一系列安全场景的解决方案
Stars: ✭ 21 (-4.55%)
Mutual labels:  authorization
Events-based-organizational-website
The official codebase for college-based (event managing) organizations. FOUR-LEVEL Authorization system and scalable.
Stars: ✭ 14 (-36.36%)
Mutual labels:  authorization
django-keeper
Authorization library for Django, with ACL, not depends on models.
Stars: ✭ 47 (+113.64%)
Mutual labels:  authorization
spring-boot-security-postgresql
Spring Boot, Spring Security, PostgreSQL: JWT Authentication & Authorization example
Stars: ✭ 65 (+195.45%)
Mutual labels:  authorization
laravel-hashid
HashId Implementation on Laravel Eloquent ORM
Stars: ✭ 23 (+4.55%)
Mutual labels:  eloquent
eloquent-phpunit
Eloquent model and database schema PHPUnit test case
Stars: ✭ 20 (-9.09%)
Mutual labels:  eloquent
praetorian
A minimalist Crystal authorization system inspired by https://github.com/varvet/pundit.
Stars: ✭ 54 (+145.45%)
Mutual labels:  authorization
userstamps
A simple package to insert and load userstamps for a model automatically, it provides an eloquent trait to use in models..
Stars: ✭ 34 (+54.55%)
Mutual labels:  eloquent
secure-oauth2-oidc-workshop
Hands-On Workshop for OAuth 2.0 and OpenID Connect 1.0
Stars: ✭ 58 (+163.64%)
Mutual labels:  authorization
laravel-repository
Repository pattern implementation for Laravel
Stars: ✭ 49 (+122.73%)
Mutual labels:  eloquent
azure-functions-auth
Authentication and Authorization for Azure Functions (with OAuth 2.0 and JWT)
Stars: ✭ 20 (-9.09%)
Mutual labels:  authorization
MercadoLivreProductsCrawler
PHP Console Crawler to Download Products from a Store on MercadoLivre.com.br
Stars: ✭ 18 (-18.18%)
Mutual labels:  eloquent
macaroons
An Erlang Macaroons library compatible with libmacaroons
Stars: ✭ 27 (+22.73%)
Mutual labels:  authorization
laravel-eloquent-relationships-gravit-designer
Graphic showing Laravel Eloquent ORM Relationships
Stars: ✭ 19 (-13.64%)
Mutual labels:  eloquent
deadbolt
Dead simple permissions for Laravel
Stars: ✭ 13 (-40.91%)
Mutual labels:  authorization
angular-authentication
An Angular application that demonstrates best practices for user authentication & authorization flows.
Stars: ✭ 122 (+454.55%)
Mutual labels:  authorization
open-psd2
An open source framework for using banking API's built for PSD2 regulation.
Stars: ✭ 20 (-9.09%)
Mutual labels:  authorization
django-sitegate
Reusable application for Django to ease sign up & sign in processes
Stars: ✭ 32 (+45.45%)
Mutual labels:  authorization

Authorized Model Attributes for Laravel

Provides ability to dynamically add $hidden and $fillable columns to the models.

Also see Laravel API Resources if that approach suits your needs.


Installation

Require the package to your Laravel project.

composer require vantage/authorized-attributes

Usage

Please note that this package falls back to the core Guard and there are some minor differences of writing the policies between Laravel versions. See the official docs at https://laravel.com/docs/authorization

Use the Vantage\AuthorizedAttributes trait

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Vantage\AuthorizedAttributes;

class Post extends Model
{
    use AuthorizedAttributes;

    /**
     * The attributes that should be fillable from requests.
     *
     * @var array
     */
    protected $fillable = ['title', 'content', 'author_id'];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array
     */
    protected $hidden = ['draft'];
}

Create and register a model policy.

<?php

namespace App\Policies;

use App\Post;
use App\User;

class PostPolicy
{
    /**
     * Determine if an draft attribute can be seen by the user.
     *
     * @param  \App\User  $user
     * @param  \App\Post  $post
     * @return bool
     */
    public function seeDraft(User $user, Post $post)
    {
    	// Post drafts can only be seen by admins and the post author
        return $user->isAdmin() || $user->created($post);
    }

    /**
     * Determine if the author_id attribute can be changed by the user.
     *
     * @param  \App\User  $user
     * @param  \App\Post  $post
     * @return bool
     */
    public function editAuthorId(User $user, Post $post)
    {
    	// Admins can re-assign the author for non-published posts
        return $user->isAdmin() && $post->isNotPublished();
    }
}

Customization

Mixin with always hidden attributes

The attributes will be hidden if no policy or ability are found as they would normally be.

Modify the ability method names

<?php

use Illuminate\Support\Str;

class Post extends Model
{
    /**
     * Get the method name for the attribute visibility ability in the model policy.
     *
     * @param  string  $attribute
     * @return string
     */
    public function getAttributeViewAbilityMethod($attribute)
    {
        return 'see'.Str::studly($attribute);
    }

    /**
     * Get the model policy ability method name to update an model attribute.
     *
     * @param  string  $attribute
     * @return string
     */
    public function getAttributeUpdateAbilityMethod($attribute)
    {
        return 'edit'.Str::studly($attribute);
    }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].