src/Database/Queries/Abilities.php
protected static function getRoleConstraint(Model $authority, $allowed)
    {
        return function ($query) use ($authority, $allowed) {
            $permissions = Models::table('permissions');
            $abilities   = Models::table('abilities');
            $roles       = Models::table('roles');
            $prefix      = Models::prefix();

            $query->from($roles)
                  ->join($permissions, $roles.'.id', '=', $permissions.'.entity_id')
                  ->whereRaw("{$prefix}{$permissions}.ability_id = {$prefix}{$abilities}.id")
                  ->where($permissions.".forbidden", ! $allowed)
                  ->where($permissions.".entity_type", Models::role()->getMorphClass());

            Models::scope()->applyToModelQuery($query, $roles);
            Models::scope()->applyToRelationQuery($query, $permissions);

            $query->where(function ($query) use ($roles, $authority, $allowed) {
                $query->whereExists(static::getAuthorityRoleConstraint($authority));

                if ($allowed) {
                    static::addRoleInheritCondition($query, $authority, $roles);
                }
            });
        };
    }
protected static function getRoleConstraint(Model $authority, $allowed)
    {
        return function ($query) use ($authority, $allowed) {
            $permissions = Models::table('permissions');
            $abilities   = Models::table('abilities');
            $roles       = Models::table('roles');
            $prefix      = Models::prefix();

            $query->from($roles)
                  ->join($permissions, $roles.'.id', '=', $permissions.'.entity_id')
                  ->whereRaw("{$prefix}{$permissions}.ability_id = {$prefix}{$abilities}.id")
                  ->where($permissions.".forbidden", ! $allowed)
                  ->where($permissions.".entity_type", Models::role()->getMorphClass());

            Models::scope()->applyToModelQuery($query, $roles);
            Models::scope()->applyToRelationQuery($query, $permissions);

            $query->where(function ($query) use ($roles, $authority, $allowed) {
                $query->whereExists(static::getAuthorityRoleConstraint($authority));

                if ($allowed) {
                    static::addRoleInheritCondition($query, $authority, $roles);
                }
            });
        };
    }
protected static function getRoleConstraint ( Model $ authority , $ allowed ) { return function ( $ query ) use ( $ authority , $ allowed ) { $ permissions = Models :: table ( 'permissions' ) ; $ abilities = Models :: table ( 'abilities' ) ; $ roles = Models :: table ( 'roles' ) ; $ prefix = Models :: prefix ( ) ; $ query -> from ( $ roles ) -> join ( $ permissions , $ roles . '.id' , '=' , $ permissions . '.entity_id' ) -> whereRaw ( "{$prefix}{$permissions}.ability_id = {$prefix}{$abilities}.id" ) -> where ( $ permissions . ".forbidden" , ! $ allowed ) -> where ( $ permissions . ".entity_type" , Models :: role ( ) -> getMorphClass ( ) ) ; Models :: scope ( ) -> applyToModelQuery ( $ query , $ roles ) ; Models :: scope ( ) -> applyToRelationQuery ( $ query , $ permissions ) ; $ query -> where ( function ( $ query ) use ( $ roles , $ authority , $ allowed ) { $ query -> whereExists ( static :: getAuthorityRoleConstraint ( $ authority ) ) ; if ( $ allowed ) { static :: addRoleInheritCondition ( $ query , $ authority , $ roles ) ; } } ) ; } ; }
Get a constraint for abilities that have been granted to the given authority through a role.

@param  \Illuminate\Database\Eloquent\Model  $authority
@param  bool  $allowed
@return \Closure
Get a constraint for abilities that have been granted to the given authority through a role .