BasePolicy.php 3.6 KB
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * 策略、授权层:基类 授权类
+-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Policies
 * @package   App\Policies
 * @author    Richer <yangzi1028@163.com>
 * @date      2022年1月7日11:35:41
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Policies;

use App\Models\User\Role;
use App\Models\User\User;
use BaconQrCode\Common\Mode;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Database\Eloquent\Model;

/**
 * Class BasePolicy
 *
 * @category  App\Policies
 * @package   App\Policies
 * @author    Richer <yangzi1028@163.com>
 * @date      2022年1月7日11:35:41
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
class BasePolicy
{
    use HandlesAuthorization;

    /**
     * Create a new policy instance.
     *
     * @return void
     */
    public function __construct()
    {
//        dump($user);

        // if ($user->isSuperAdmin()) {
        //     return true;
        // }
    }

    /**
     * Determine if the user has super authority
     * 在 Policy 中会在所有方法执行前调用,经常用到的地方就是处理管理员授权逻辑。
     *
     * @param User $user
     * @param Model $ability
     * @return void
     */
    public function before($user, $ability)
    {
        // if ($user->isSuperAdmin()) {
        //     return true;
        // }
    }

    /**
     * Determine if the given Model can be updated by the user.
     *
     * @param User $user
     * @param Model $model
     * @return bool
     */
    public function view(User $user, $model)
    {
        return true;
    }

    /**
     * Determine the user can create the Model.
     *
     * @param User $user
     * @return bool
     */
    public function create(User $user)
    {
        switch (true) {
            case $user->isAdmin():// 如果是管理者
                return true;
            case $user->isEmployee():// 如果是员工
                $permission     = __FUNCTION__ .'_'.Model::OBJ_NAME;
                // 获取权限
                $permissions    = $user->permission;
                if ($permissions && in_array($permission, $permissions)) {
                    return true;
                }
                break;
        }

        return false;
    }

    /**
     * Determine if the given Model can be updated by the user.
     * 1、只有我创建的才能编辑
     * 2、经理或者老板可以编辑
     *
     * @param User $user
     * @param Model $model
     * @return bool
     */
    public function update(User $user, $model)
    {
        if ($model->user_id == $user->id) {
            return true;
        }
        return false;
    }

    /**
     * Determine if the given Model can be delete by the user.
     *
     * @param User $user
     * @param Model $model
     * @return bool
     */
    public function delete(User $user, $model)
    {
        if ($model->user_id == $user->id) {
            return true;
        }

        return false;
    }

    /**
     * Determine if the given Model  be own by the user.
     *
     * @param User $user
     * @param Model $model
     * @return bool
     */
    public function own(User $user, $model)
    {
        return $model->user_id == $user->id;
    }
}