2017 © Pedro Peláez
 

library authorized-attributes

Authorized Model Attributes for Laravel

image

salomoni/authorized-attributes

Authorized Model Attributes for Laravel

  • Monday, July 23, 2018
  • by salomoni
  • Repository
  • 1 Watchers
  • 10 Stars
  • 171 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 6 Versions
  • 4 % Grown

The README.md

Authorized Model Attributes for Laravel

Provides ability to dynamically add $hidden and $fillable columns to the models., (*1)

Also see Laravel API Resources if that approach suits your needs., (*2)


Installation

Require the package to your Laravel project., (*3)

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, (*4)

Use the Vantage\AuthorizedAttributes trait, (*5)

<?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., (*6)

<?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., (*7)

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);
    }
}

The Versions

23/07 2018

dev-master

9999999-dev

Authorized Model Attributes for Laravel

  Sources   Download

MIT

The Requires

 

by Jari Pekkala

laravel authorization attributes hidden

22/05 2018

v2.0.1

2.0.1.0

Authorized Model Attributes for Laravel

  Sources   Download

MIT

The Requires

 

by Jari Pekkala

laravel authorization attributes hidden

22/05 2018

v2.0.0

2.0.0.0

Authorized Model Attributes for Laravel

  Sources   Download

MIT

The Requires

 

by Jari Pekkala

laravel authorization attributes hidden

02/05 2018

v1.0.2

1.0.2.0

Authorized Model Attributes for Laravel

  Sources   Download

MIT

The Requires

 

by Jari Pekkala

laravel authorization attributes hidden

06/09 2017

v1.0.1

1.0.1.0

Authorized Model Attributes for Laravel

  Sources   Download

MIT

The Requires

 

by Jari Pekkala

laravel authorization attributes hidden

06/09 2017

v1.0.0

1.0.0.0

Dynamic Model attribute visibility for Laravel.

  Sources   Download

MIT

The Requires

 

by Jari Pekkala

laravel authorization attributes hidden