Get the IDs of the provided abilities.

@param  \Illuminate\Database\Eloquent\model|array|int  $abilities
@param  \Illuminate\Database\Eloquent\Model|string|array|null  $model
@param  array  $attributes
@return array
17469
protected function getAbilityIds($abilities, $model = null, array $attributes = [])
    {
        if ($abilities instanceof Model) {
            return [$abilities->getKey()];
        }

        if ( ! is_null($model)) {
            return $this->getModelAbilityKeys($abilities, $model, $attributes);
        }

        if (Helpers::isAssociativeArray($abilities)) {
            return $this->getAbilityIdsFromMap($abilities, $attributes);
        }

        if (! is_array($abilities) && ! $abilities instanceof Collection) {
            $abilities = [$abilities];
        }

        return $this->getAbilityIdsFromArray($abilities, $attributes);
    }
1146964
private function mapAttributeValues(array $attributes, Attribute $attribute)
    {
        $key = $attribute->getName();
        $value = $attribute->getAllAttributeValues();

        if (!array_key_exists($key, $attributes) && 1 === count($value)) {
            $value = array_shift($value);
        }

        if (array_key_exists($key, $attributes)) {
            $currentValue = (is_array($attributes[$key]) ? $attributes[$key] : [$attributes[$key]]);

            $value = array_merge($currentValue, $value);
        }

        $attributes[$key] = $value;

        return $attributes;
    }
972237
public function to($ability, array $attributes = [])
    {
        $this->ability = $ability;

        $this->attributes = array_merge($this->attributes, $attributes);
    }
569186
private function getPermissionsByModel(string $model) : array
    {
        $table = TableRegistry::getTableLocator()->get('RolesCapabilities.Capabilities');
        Assert::isInstanceOf($table, CapabilitiesTable::class);

        $groups = $table->getUserGroups($this->user['id']);

        $query = TableRegistry::getTableLocator()->get('RolesCapabilities.Permissions')
            ->find('all')
            ->select('foreign_key')
            ->where([
                // WARNING: this might conflict with APP table's name matching a plugin's table name
                'model' => $model,
                'type IN ' => ['view'],
                'OR' => [
                    ['owner_foreign_key IN ' => array_keys($groups), 'owner_model' => 'Groups'],
                    ['owner_foreign_key' => $this->user['id'], 'owner_model' => 'Users']
                ]
            ])
            ->applyOptions(['accessCheck' => false]);

        $result = [];
        foreach ($query->all() as $permission) {
            $result[] = $permission->get('foreign_key');
        }

        return $result;
    }
125083
public function getByAttributes(array $attributes) : ?Model
    {
        if (empty($attributes)) {
            return null;
        }

        // First we will add each credential element to the query as a where clause.
        // Then we can execute the query and, if we found a user, return it in a
        // Eloquent User "model" that will be utilized by the Guard instances.
        $query = $this->getModel()->newQuery();

        foreach ($attributes as $key => $value) {
            $query->where($key, $value);
        }

        return $query->first();
    }
247739
public function getAttributes($model, array $fields = null)
    {
        $attributes = $this->attributes($model);

        if ($fields !== null) {
            $attributes = array_only($attributes, $fields);
        }

        return $attributes;
    }
251892
protected function abilitiesByName($abilities, $attributes = [])
    {
        $abilities = array_unique(is_array($abilities) ? $abilities : [$abilities]);

        if (empty($abilities)) {
            return new Collection;
        }

        $existing = Models::ability()->simpleAbility()->whereIn('name', $abilities)->get();

        return $existing->merge($this->createMissingAbilities(
            $existing, $abilities, $attributes
        ));
    }
917237
protected function isForbidden(Model $authority, $ability, $model = null)
    {
        return $this->getHasAbilityQuery(
            $authority, $ability, $model, $allowed = false
        )->exists();
    }
942366
public function associate($model, $attributes = array())
    {
       $otherKey = ($model instanceof Model ? $model->getAttribute($this->otherKey) : $model);

        $this->parent->setAttribute($this->foreignKey, $otherKey);

        if ($model instanceof Model) {
            $this->parent->setRelation($this->relation, $model);
        }

        return $this->parent;
    }
1101339
protected function isGeneralManagementAbility(Model $ability)
    {
        return $ability->name === '*'
            && $ability->entity_type !== '*'
            && ! is_null($ability->entity_type)
            && is_null($ability->entity_id);
    }
1224806
public function associateAbilityToResources(Ability $ability, array $nodes)
    {
        $associatedNodes = [];

        foreach ($nodes as $node) {
            if (!$ability->isLinkedToResource($node)) {
                $ability->linkResource($node);
                $associatedNodes[] = $node;
            }
        }
        $this->om->persist($ability);
        $this->om->flush();

        return $associatedNodes;
    }
191393
private function _getJoins( array $attributes, $prefix )
	{
		$iface = 'MW_Common_Criteria_Attribute_Interface';
		$sep = $this->_getKeySeparator();
		$name = $prefix . $sep . 'id';

		if( isset( $attributes[$name] ) && $attributes[$name] instanceof $iface ) {
			return $attributes[$name]->getInternalDeps();
		}
		else if( isset( $attributes['id'] ) && $attributes['id'] instanceof $iface ) {
			return $attributes['id']->getInternalDeps();
		}

		return array();
	}
409682
protected function syncAbilities($abilities, $options = ['forbidden' => false])
    {
        $abilityKeys = $this->getAbilityIds($abilities);
        $authority = $this->getAuthority();
        $relation = $authority->abilities();

        $this->newPivotQuery($relation)
             ->where('entity_type', $authority->getMorphClass())
             ->whereNotIn($this->getRelatedPivotKeyName($relation), $abilityKeys)
             ->where('forbidden', $options['forbidden'])
             ->delete();

        if ($options['forbidden']) {
            (new ForbidsAbilities($this->authority))->to($abilityKeys);
        } else {
            (new GivesAbilities($this->authority))->to($abilityKeys);
        }
    }
93377
protected function getAttributes( array $attributes ) : array
   {

      $result = [];

      foreach ( $attributes as $attribute )
      {
         if ( ! ( $attribute instanceof Descriptor ) )
         {
            continue;
         }
         $result[ $attribute->getName() ] = $attribute;
      }

      return $result;

   }
921199
protected function createMissingAbilities($existing, array $abilities, $attributes = [])
    {
        $missing = array_diff($abilities, $existing->pluck('name')->all());

        return array_map(function ($ability) use ($attributes) {
            return Models::ability()->create($attributes + ['name' => $ability]);
        }, $missing);
    }
1124918
private function runGateDefine(array $abilities_permissions)
    {
        foreach ($abilities_permissions as $ability => $policies) {
            $this->gate->define($ability, function ($user, $model = null) use ($ability, $policies) {
                $result = true;
                foreach ($policies as $restriction) {
                    if (is_array($restriction)) {
                        $policy = reset($restriction);
                        $operator = key($restriction);
                    } else {
                        $policy = $restriction;
                        $operator = 'and';
                    }

                    if (is_callable($policy)) {
                        $policy_method_return = call_user_func($policy, $user, $ability, $model);
                    } else {
                        list($class, $method) = explode('@', $policy);
                        $policy_method_return = app($class)->$method($user, $ability, $model); // should return boolean
                    }

                    if ($operator === 'or' || $operator === '||') {
                        $result = $result || $policy_method_return;
                    } else {
                        $result = $result && $policy_method_return;
                    }
                }

                return $result;
            });
        }
    }
1023654
public function update($id, $attributes)
    {
        $attributes = $this->castRequest($attributes);
        $updated    = false;
        $instance   = $id instanceof Model ? $id : $this->find($id);

        if ($instance) {
            event(implode('-', $this->tag).'.entity.updating', [$this, $instance, $attributes]);

            $updated = $instance->update($attributes);

            event(implode('-', $this->tag).'.entity.updated', [$this, $instance, $attributes]);
        }

        return [
            $updated,
            $instance,
        ];
    }