idx
int64
0
60.3k
question
stringlengths
101
6.21k
target
stringlengths
7
803
3,800
protected static function getAuthorityRoleConstraint ( Model $ authority ) { return function ( $ query ) use ( $ authority ) { $ pivot = Models :: table ( 'assigned_roles' ) ; $ roles = Models :: table ( 'roles' ) ; $ table = $ authority -> getTable ( ) ; $ prefix = Models :: prefix ( ) ; $ query -> from ( $ table ) -> join ( $ pivot , "{$table}.{$authority->getKeyName()}" , '=' , $ pivot . '.entity_id' ) -> whereRaw ( "{$prefix}{$pivot}.role_id = {$prefix}{$roles}.id" ) -> where ( $ pivot . '.entity_type' , $ authority -> getMorphClass ( ) ) -> where ( "{$table}.{$authority->getKeyName()}" , $ authority -> getKey ( ) ) ; Models :: scope ( ) -> applyToModelQuery ( $ query , $ roles ) ; Models :: scope ( ) -> applyToRelationQuery ( $ query , $ pivot ) ; } ; }
Get a constraint for roles that are assigned to the given authority .
3,801
protected static function getAuthorityConstraint ( Model $ authority , $ allowed ) { return function ( $ query ) use ( $ authority , $ allowed ) { $ permissions = Models :: table ( 'permissions' ) ; $ abilities = Models :: table ( 'abilities' ) ; $ table = $ authority -> getTable ( ) ; $ prefix = Models :: prefix ( ) ; $ query -> from ( $ table ) -> join ( $ permissions , "{$table}.{$authority->getKeyName()}" , '=' , $ permissions . '.entity_id' ) -> whereRaw ( "{$prefix}{$permissions}.ability_id = {$prefix}{$abilities}.id" ) -> where ( "{$permissions}.forbidden" , ! $ allowed ) -> where ( "{$permissions}.entity_type" , $ authority -> getMorphClass ( ) ) -> where ( "{$table}.{$authority->getKeyName()}" , $ authority -> getKey ( ) ) ; Models :: scope ( ) -> applyToModelQuery ( $ query , $ abilities ) ; Models :: scope ( ) -> applyToRelationQuery ( $ query , $ permissions ) ; } ; }
Get a constraint for abilities that have been granted to the given authority .
3,802
protected static function getEveryoneConstraint ( $ allowed ) { return function ( $ query ) use ( $ allowed ) { $ permissions = Models :: table ( 'permissions' ) ; $ abilities = Models :: table ( 'abilities' ) ; $ prefix = Models :: prefix ( ) ; $ query -> from ( $ permissions ) -> whereRaw ( "{$prefix}{$permissions}.ability_id = {$prefix}{$abilities}.id" ) -> where ( "{$permissions}.forbidden" , ! $ allowed ) -> whereNull ( 'entity_id' ) ; Models :: scope ( ) -> applyToRelationQuery ( $ query , $ permissions ) ; } ; }
Get a constraint for abilities that have been granted to everyone .
3,803
public function to ( $ abilities , $ entity = null , array $ attributes = [ ] ) { if ( call_user_func_array ( [ $ this , 'shouldConductLazy' ] , func_get_args ( ) ) ) { return $ this -> conductLazy ( $ abilities ) ; } if ( $ ids = $ this -> getAbilityIds ( $ abilities , $ entity , $ attributes ) ) { $ this -> disassociateAbilities ( $ this -> getAuthority ( ) , $ ids ) ; } return true ; }
Remove the given ability from the model .
3,804
protected function disassociateAbilities ( $ authority , array $ ids ) { if ( is_null ( $ authority ) ) { $ this -> disassociateEveryone ( $ ids ) ; } else { $ this -> disassociateAuthority ( $ authority , $ ids ) ; } }
Detach the given IDs from the authority .
3,805
protected function disassociateAuthority ( Model $ authority , array $ ids ) { $ this -> getAbilitiesPivotQuery ( $ authority , $ ids ) -> where ( $ this -> constraints ( ) ) -> delete ( ) ; }
Disassociate the authority from the abilities with the given IDs .
3,806
protected function getAbilitiesPivotQuery ( Model $ model , $ ids ) { $ relation = $ model -> abilities ( ) ; list ( $ foreignKeyName , $ relatedKeyName ) = $ this -> getRelationKeyNames ( $ relation ) ; $ query = $ relation -> newPivotStatement ( ) -> where ( $ foreignKeyName , $ model -> getKey ( ) ) -> where ( 'entity_type' , $ model -> getMorphClass ( ) ) -> whereIn ( $ relatedKeyName , $ ids ) ; return Models :: scope ( ) -> applyToRelationQuery ( $ query , $ relation -> getTable ( ) ) ; }
Get the base abilities pivot query .
3,807
protected function getRelationKeyNames ( MorphToMany $ relation ) { if ( method_exists ( $ relation , 'getForeignKey' ) ) { return [ $ relation -> getForeignKey ( ) , $ relation -> getOtherKey ( ) , ] ; } if ( method_exists ( $ relation , 'getQualifiedForeignKeyName' ) ) { return [ $ relation -> getQualifiedForeignKeyName ( ) , $ relation -> getQualifiedRelatedKeyName ( ) , ] ; } return [ $ relation -> getQualifiedForeignPivotKeyName ( ) , $ relation -> getQualifiedRelatedPivotKeyName ( ) , ] ; }
Get the two primary key names from the relation .
3,808
protected function disassociateEveryone ( array $ ids ) { $ query = Models :: query ( 'permissions' ) -> whereNull ( 'entity_id' ) -> where ( $ this -> constraints ( ) ) -> whereIn ( 'ability_id' , $ ids ) ; Models :: scope ( ) -> applyToRelationQuery ( $ query , $ query -> from ) ; $ query -> delete ( ) ; }
Disassociate everyone from the abilities with the given IDs .
3,809
protected function getAuthority ( ) { if ( is_null ( $ this -> authority ) ) { return null ; } if ( $ this -> authority instanceof Model ) { return $ this -> authority ; } return Models :: role ( ) -> where ( 'name' , $ this -> authority ) -> firstOrFail ( ) ; }
Get the authority from which to disassociate the abilities .
3,810
protected function humanize ( $ value ) { $ value = str_replace ( ' ' , '_' , $ value ) ; $ value = Str :: snake ( $ value ) ; $ value = preg_replace ( '~(?:-|_)+~' , ' ' , $ value ) ; $ value = preg_replace ( '~([^ ])(?:#)+~' , '$1 #' , $ value ) ; return ucfirst ( $ value ) ; }
Convert the given string into a human - readable format .
3,811
public function roles ( $ roles ) { $ this -> sync ( Models :: role ( ) -> getRoleKeys ( $ roles ) , $ this -> authority -> roles ( ) ) ; }
Sync the provided roles to the authority .
3,812
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 ) ; } }
Sync the given abilities for the authority .
3,813
protected function sync ( array $ ids , BelongsToMany $ relation ) { $ current = $ this -> pluck ( $ this -> newPivotQuery ( $ relation ) , $ this -> getRelatedPivotKeyName ( $ relation ) ) ; $ this -> detach ( array_diff ( $ current , $ ids ) , $ relation ) ; $ relation -> attach ( array_diff ( $ ids , $ current ) , Models :: scope ( ) -> getAttachAttributes ( $ this -> authority ) ) ; }
Sync the given IDs on the pivot relation .
3,814
public function detach ( array $ ids , BelongsToMany $ relation ) { if ( empty ( $ ids ) ) { return ; } $ this -> newPivotQuery ( $ relation ) -> whereIn ( $ this -> getRelatedPivotKeyName ( $ relation ) , $ ids ) -> delete ( ) ; }
Detach the records with the given IDs from the relationship .
3,815
protected function newPivotQuery ( BelongsToMany $ relation ) { $ query = $ relation -> newPivotStatement ( ) -> where ( $ this -> getForeignPivotKeyName ( $ relation ) , $ relation -> getParent ( ) -> getKey ( ) ) ; return Models :: scope ( ) -> applyToRelationQuery ( $ query , $ relation -> getTable ( ) ) ; }
Get a scoped query for the pivot table .
3,816
protected function getForeignPivotKeyName ( BelongsToMany $ relation ) { if ( method_exists ( $ relation , 'getForeignPivotKeyName' ) ) { return $ relation -> getForeignPivotKeyName ( ) ; } if ( method_exists ( $ relation , 'getQualifiedForeignKeyName' ) ) { return $ relation -> getQualifiedForeignKeyName ( ) ; } return $ relation -> getForeignKey ( ) ; }
Get the column name for the foreign key on the pivot table .
3,817
protected function getRelatedPivotKeyName ( BelongsToMany $ relation ) { if ( method_exists ( $ relation , 'getRelatedPivotKeyName' ) ) { return $ relation -> getRelatedPivotKeyName ( ) ; } if ( method_exists ( $ relation , 'getQualifiedRelatedKeyName' ) ) { return $ relation -> getQualifiedRelatedKeyName ( ) ; } return $ relation -> getOtherKey ( ) ; }
Get the column name for the related key on the pivot table .
3,818
protected function pluck ( $ query , $ column ) { return Arr :: pluck ( $ query -> get ( [ $ column ] ) , last ( explode ( '.' , $ column ) ) ) ; }
Pluck the values of the given column using the provided query .
3,819
public function to ( $ authority ) { $ authorities = is_array ( $ authority ) ? $ authority : [ $ authority ] ; $ roles = Models :: role ( ) -> findOrCreateRoles ( $ this -> roles ) ; foreach ( Helpers :: mapAuthorityByClass ( $ authorities ) as $ class => $ ids ) { $ this -> assignRoles ( $ roles , $ class , new Collection ( $ ids ) ) ; } return true ; }
Assign the roles to the given authority .
3,820
protected function assignRoles ( Collection $ roles , $ authorityClass , Collection $ authorityIds ) { $ roleIds = $ roles -> map ( function ( $ model ) { return $ model -> getKey ( ) ; } ) ; $ morphType = ( new $ authorityClass ) -> getMorphClass ( ) ; $ records = $ this -> buildAttachRecords ( $ roleIds , $ morphType , $ authorityIds ) ; $ existing = $ this -> getExistingAttachRecords ( $ roleIds , $ morphType , $ authorityIds ) ; $ this -> createMissingAssignRecords ( $ records , $ existing ) ; }
Assign the given roles to the given authorities .
3,821
protected function getExistingAttachRecords ( $ roleIds , $ morphType , $ authorityIds ) { $ query = $ this -> newPivotTableQuery ( ) -> whereIn ( 'role_id' , $ roleIds -> all ( ) ) -> whereIn ( 'entity_id' , $ authorityIds -> all ( ) ) -> where ( 'entity_type' , $ morphType ) ; Models :: scope ( ) -> applyToRelationQuery ( $ query , $ query -> from ) ; return new Collection ( $ query -> get ( ) ) ; }
Get the pivot table records for the roles already assigned .
3,822
protected function buildAttachRecords ( $ roleIds , $ morphType , $ authorityIds ) { return $ roleIds -> map ( function ( $ roleId ) use ( $ morphType , $ authorityIds ) { return $ authorityIds -> map ( function ( $ authorityId ) use ( $ roleId , $ morphType ) { return Models :: scope ( ) -> getAttachAttributes ( ) + [ 'role_id' => $ roleId , 'entity_id' => $ authorityId , 'entity_type' => $ morphType , ] ; } ) ; } ) -> collapse ( ) ; }
Build the raw attach records for the assigned roles pivot table .
3,823
protected function createMissingAssignRecords ( Collection $ records , Collection $ existing ) { $ existing = $ existing -> keyBy ( function ( $ record ) { return $ this -> getAttachRecordHash ( ( array ) $ record ) ; } ) ; $ records = $ records -> reject ( function ( $ record ) use ( $ existing ) { return $ existing -> has ( $ this -> getAttachRecordHash ( $ record ) ) ; } ) ; $ this -> newPivotTableQuery ( ) -> insert ( $ records -> all ( ) ) ; }
Save the non - existing attach records in the DB .
3,824
public function setClipboard ( Contracts \ Clipboard $ clipboard ) { $ this -> guard -> setClipboard ( $ clipboard ) ; return $ this -> registerClipboardAtContainer ( ) ; }
Set the clipboard instance used by bouncer .
3,825
public function registerClipboardAtContainer ( ) { $ clipboard = $ this -> guard -> getClipboard ( ) ; Container :: getInstance ( ) -> instance ( Contracts \ Clipboard :: class , $ clipboard ) ; return $ this ; }
Register the guard s clipboard at the container .
3,826
public function cache ( Store $ cache = null ) { $ cache = $ cache ? : $ this -> resolve ( CacheRepository :: class ) -> getStore ( ) ; if ( $ this -> usesCachedClipboard ( ) ) { $ this -> guard -> getClipboard ( ) -> setCache ( $ cache ) ; return $ this ; } return $ this -> setClipboard ( new CachedClipboard ( $ cache ) ) ; }
Use a cached clipboard with the given cache instance .
3,827
public function constrainWhereIsAll ( $ query , $ role ) { $ roles = array_slice ( func_get_args ( ) , 1 ) ; return $ query -> whereHas ( 'roles' , function ( $ query ) use ( $ roles ) { $ query -> whereIn ( 'name' , $ roles ) ; } , '=' , count ( $ roles ) ) ; }
Constrain the given query by all provided roles .
3,828
public function constrainWhereAssignedTo ( $ query , $ model , array $ keys = null ) { list ( $ model , $ keys ) = Helpers :: extractModelAndKeys ( $ model , $ keys ) ; $ query -> whereExists ( function ( $ query ) use ( $ model , $ keys ) { $ table = $ model -> getTable ( ) ; $ key = "{$table}.{$model->getKeyName()}" ; $ pivot = Models :: table ( 'assigned_roles' ) ; $ roles = Models :: table ( 'roles' ) ; $ prefix = Models :: prefix ( ) ; $ query -> from ( $ table ) -> join ( $ pivot , $ key , '=' , $ pivot . '.entity_id' ) -> whereRaw ( "{$prefix}{$pivot}.role_id = {$prefix}{$roles}.id" ) -> where ( "{$pivot}.entity_type" , $ model -> getMorphClass ( ) ) -> whereIn ( $ key , $ keys ) ; Models :: scope ( ) -> applyToModelQuery ( $ query , $ roles ) ; Models :: scope ( ) -> applyToRelationQuery ( $ query , $ pivot ) ; } ) ; }
Constrain the given roles query to those that were assigned to the given authorities .
3,829
public function abilities ( ) { $ relation = $ this -> morphToMany ( Models :: classname ( Ability :: class ) , 'entity' , Models :: table ( 'permissions' ) ) -> withPivot ( 'forbidden' , 'scope' ) ; return Models :: scope ( ) -> applyToRelation ( $ relation ) ; }
The abilities relationship .
3,830
public function allow ( $ ability = null , $ model = null ) { if ( is_null ( $ ability ) ) { return new GivesAbilities ( $ this ) ; } ( new GivesAbilities ( $ this ) ) -> to ( $ ability , $ model ) ; return $ this ; }
Give an ability to the model .
3,831
public function disallow ( $ ability = null , $ model = null ) { if ( is_null ( $ ability ) ) { return new RemovesAbilities ( $ this ) ; } ( new RemovesAbilities ( $ this ) ) -> to ( $ ability , $ model ) ; return $ this ; }
Remove an ability from the model .
3,832
public function forbid ( $ ability = null , $ model = null ) { if ( is_null ( $ ability ) ) { return new ForbidsAbilities ( $ this ) ; } ( new ForbidsAbilities ( $ this ) ) -> to ( $ ability , $ model ) ; return $ this ; }
Forbid an ability to the model .
3,833
public function unforbid ( $ ability = null , $ model = null ) { if ( is_null ( $ ability ) ) { return new UnforbidsAbilities ( $ this ) ; } ( new UnforbidsAbilities ( $ this ) ) -> to ( $ ability , $ model ) ; return $ this ; }
Remove ability forbiddal from the model .
3,834
protected function isGeneralManagementAbility ( Model $ ability ) { return $ ability -> name === '*' && $ ability -> entity_type !== '*' && ! is_null ( $ ability -> entity_type ) && is_null ( $ ability -> entity_id ) ; }
Determines whether the given ability is for managing all models of a given type .
3,835
protected function isBlanketModelAbility ( Model $ ability ) { return $ ability -> name !== '*' && $ ability -> entity_type !== '*' && ! is_null ( $ ability -> entity_type ) && is_null ( $ ability -> entity_id ) ; }
Determines whether the given ability is for an action on all models of a given type .
3,836
protected function isSpecificModelAbility ( Model $ ability ) { return $ ability -> entity_type !== '*' && ! is_null ( $ ability -> entity_type ) && ! is_null ( $ ability -> entity_id ) ; }
Determines whether the given ability is for an action on a specific model .
3,837
protected function isGlobalActionAbility ( Model $ ability ) { return $ ability -> name !== '*' && $ ability -> entity_type === '*' && is_null ( $ ability -> entity_id ) ; }
Determines whether the given ability allows an action on all models .
3,838
protected function getBlanketModelAbilityTitle ( Model $ ability , $ name = 'manage' ) { return $ this -> humanize ( $ name . ' ' . $ this -> getPluralName ( $ ability -> entity_type ) ) ; }
Get the title for the given blanket model ability .
3,839
protected function getSpecificModelAbilityTitle ( Model $ ability ) { $ name = $ ability -> name === '*' ? 'manage' : $ ability -> name ; return $ this -> humanize ( $ name . ' ' . $ this -> basename ( $ ability -> entity_type ) . ' #' . $ ability -> entity_id ) ; }
Get the title for the given model ability .
3,840
public static function fromData ( array $ data ) { $ group = new static ( array_map ( function ( $ data ) { return $ data [ 'class' ] :: fromData ( $ data [ 'params' ] ) ; } , $ data [ 'constraints' ] ) ) ; return $ group -> logicalOperator ( $ data [ 'logicalOperator' ] ) ; }
Create a new instance from the raw data .
3,841
public function equals ( Constrainer $ constrainer ) { if ( ! $ constrainer instanceof static ) { return false ; } if ( $ this -> constraints -> count ( ) != $ constrainer -> constraints -> count ( ) ) { return false ; } foreach ( $ this -> constraints as $ index => $ constraint ) { if ( ! $ constrainer -> constraints [ $ index ] -> equals ( $ constraint ) ) { return false ; } } return true ; }
Determine whether the given constrainer is equal to this object .
3,842
public function data ( ) { return [ 'class' => static :: class , 'params' => [ 'logicalOperator' => $ this -> logicalOperator , 'constraints' => $ this -> constraints -> map ( function ( $ constraint ) { return $ constraint -> data ( ) ; } ) -> all ( ) , ] , ] ; }
Get the JSON - able data of this object .
3,843
public function check ( Model $ authority , $ ability , $ model = null ) { return ( bool ) $ this -> checkGetId ( $ authority , $ ability , $ model ) ; }
Determine if the given authority has the given ability .
3,844
public function checkRole ( Model $ authority , $ roles , $ boolean = 'or' ) { $ count = $ this -> countMatchingRoles ( $ authority , $ roles ) ; if ( $ boolean == 'or' ) { return $ count > 0 ; } elseif ( $ boolean === 'not' ) { return $ count === 0 ; } return $ count == count ( ( array ) $ roles ) ; }
Check if an authority has the given roles .
3,845
protected function countMatchingRoles ( $ authority , $ roles ) { $ lookups = $ this -> getRolesLookup ( $ authority ) ; return count ( array_filter ( $ roles , function ( $ role ) use ( $ lookups ) { switch ( true ) { case is_string ( $ role ) : return $ lookups [ 'names' ] -> has ( $ role ) ; case is_numeric ( $ role ) : return $ lookups [ 'ids' ] -> has ( $ role ) ; case $ role instanceof Model : return $ lookups [ 'ids' ] -> has ( $ role -> getKey ( ) ) ; } throw new InvalidArgumentException ( 'Invalid model identifier' ) ; } ) ) ; }
Count the authority s roles matching the given roles .
3,846
public function getAbilities ( Model $ authority , $ allowed = true ) { return Abilities :: forAuthority ( $ authority , $ allowed ) -> get ( ) ; }
Get a list of the authority s abilities .
3,847
public function can ( $ ability , $ model = null ) { return $ this -> getClipboardInstance ( ) -> check ( $ this , $ ability , $ model ) ; }
Determine if the authority has a given ability .
3,848
public function slot ( $ slot = null ) { if ( is_null ( $ slot ) ) { return $ this -> slot ; } if ( ! in_array ( $ slot , [ 'before' , 'after' ] ) ) { throw new InvalidArgumentException ( "{$slot} is an invalid gate slot" ) ; } $ this -> slot = $ slot ; return $ this ; }
Set or get which slot to run the clipboard s checks .
3,849
public function registerAt ( Gate $ gate ) { $ gate -> before ( function ( ) { return call_user_func_array ( [ $ this , 'runBeforeCallback' ] , func_get_args ( ) ) ; } ) ; $ gate -> after ( function ( ) { return call_user_func_array ( [ $ this , 'runAfterCallback' ] , func_get_args ( ) ) ; } ) ; return $ this ; }
Register the clipboard at the given gate .
3,850
protected function parseGateBeforeArguments ( $ arguments , $ additional ) { if ( ! is_null ( $ additional ) ) { return [ $ arguments , $ additional ] ; } if ( is_array ( $ arguments ) ) { return [ isset ( $ arguments [ 0 ] ) ? $ arguments [ 0 ] : null , isset ( $ arguments [ 1 ] ) ? $ arguments [ 1 ] : null , ] ; } return [ $ arguments , null ] ; }
Parse the arguments we got from the gate .
3,851
protected function checkAtClipboard ( Model $ authority , $ ability , $ model ) { if ( $ id = $ this -> clipboard -> checkGetId ( $ authority , $ ability , $ model ) ) { return $ this -> allow ( 'Bouncer granted permission via ability #' . $ id ) ; } return $ id ; }
Run an auth check at the clipboard .
3,852
public static function bootIsRole ( ) { BaseTenantScope :: register ( static :: class ) ; static :: creating ( function ( $ role ) { Models :: scope ( ) -> applyToModel ( $ role ) ; if ( is_null ( $ role -> title ) ) { $ role -> title = RoleTitle :: from ( $ role ) -> toString ( ) ; } } ) ; static :: deleted ( function ( $ role ) { $ role -> abilities ( ) -> detach ( ) ; } ) ; }
Boot the is role trait .
3,853
public function users ( ) { $ relation = $ this -> morphedByMany ( Models :: classname ( User :: class ) , 'entity' , Models :: table ( 'assigned_roles' ) ) -> withPivot ( 'scope' ) ; return Models :: scope ( ) -> applyToRelation ( $ relation ) ; }
The users relationship .
3,854
public function findOrCreateRoles ( $ roles ) { $ roles = Helpers :: groupModelsAndIdentifiersByType ( $ roles ) ; $ roles [ 'integers' ] = $ this -> find ( $ roles [ 'integers' ] ) ; $ roles [ 'strings' ] = $ this -> findOrCreateRolesByName ( $ roles [ 'strings' ] ) ; return $ this -> newCollection ( Arr :: collapse ( $ roles ) ) ; }
Find the given roles creating the names that don t exist yet .
3,855
protected function findOrCreateRolesByName ( $ names ) { if ( empty ( $ names ) ) { return [ ] ; } $ existing = static :: whereIn ( 'name' , $ names ) -> get ( ) -> keyBy ( 'name' ) ; return ( new Collection ( $ names ) ) -> diff ( $ existing -> pluck ( 'name' ) ) -> map ( function ( $ name ) { return static :: create ( compact ( 'name' ) ) ; } ) -> merge ( $ existing ) ; }
Find roles by name creating the ones that don t exist .
3,856
public function getRoleKeys ( $ roles ) { $ roles = Helpers :: groupModelsAndIdentifiersByType ( $ roles ) ; $ roles [ 'strings' ] = $ this -> getKeysByName ( $ roles [ 'strings' ] ) ; $ roles [ 'models' ] = Arr :: pluck ( $ roles [ 'models' ] , $ this -> getKeyName ( ) ) ; return Arr :: collapse ( $ roles ) ; }
Get the IDs of the given roles .
3,857
public function getRoleNames ( $ roles ) { $ roles = Helpers :: groupModelsAndIdentifiersByType ( $ roles ) ; $ roles [ 'integers' ] = $ this -> getNamesByKey ( $ roles [ 'integers' ] ) ; $ roles [ 'models' ] = Arr :: pluck ( $ roles [ 'models' ] , 'name' ) ; return Arr :: collapse ( $ roles ) ; }
Get the names of the given roles .
3,858
public function getKeysByName ( $ names ) { if ( empty ( $ names ) ) { return [ ] ; } return $ this -> whereIn ( 'name' , $ names ) -> select ( $ this -> getKeyName ( ) ) -> get ( ) -> pluck ( $ this -> getKeyName ( ) ) -> all ( ) ; }
Get the keys of the roles with the given names .
3,859
public function getNamesByKey ( $ keys ) { if ( empty ( $ keys ) ) { return [ ] ; } return $ this -> whereIn ( $ this -> getKeyName ( ) , $ keys ) -> select ( 'name' ) -> get ( ) -> pluck ( 'name' ) -> all ( ) ; }
Get the names of the roles with the given IDs .
3,860
protected function createAssignRecords ( Model $ model , array $ keys ) { $ type = $ model -> getMorphClass ( ) ; return array_map ( function ( $ key ) use ( $ type ) { return Models :: scope ( ) -> getAttachAttributes ( ) + [ 'role_id' => $ this -> getKey ( ) , 'entity_type' => $ type , 'entity_id' => $ key , ] ; } , $ keys ) ; }
Create the pivot table records for assigning the role to given models .
3,861
public function scopeWhereAssignedTo ( $ query , $ model , array $ keys = null ) { ( new RolesQuery ) -> constrainWhereAssignedTo ( $ query , $ model , $ keys ) ; }
Constrain the given query to roles that were assigned to the given authorities .
3,862
protected function shouldConductLazy ( $ abilities ) { if ( func_num_args ( ) > 1 ) { return false ; } if ( is_string ( $ abilities ) ) { return true ; } if ( ! is_array ( $ abilities ) || ! Helpers :: isIndexedArray ( $ abilities ) ) { return false ; } return count ( array_filter ( $ abilities , 'is_string' ) ) == count ( $ abilities ) ; }
Determines whether a call to to with the given parameters should be conducted lazily .
3,863
protected function getComputedOptions ( ) { $ unassigned = $ this -> option ( 'unassigned' ) ; $ orphaned = $ this -> option ( 'orphaned' ) ; if ( ! $ unassigned && ! $ orphaned ) { $ unassigned = $ orphaned = true ; } return [ $ unassigned , $ orphaned ] ; }
Get the options to use computed by omission .
3,864
protected function deleteUnassignedAbilities ( ) { $ query = $ this -> getUnassignedAbilitiesQuery ( ) ; if ( ( $ count = $ query -> count ( ) ) > 0 ) { $ query -> delete ( ) ; $ this -> info ( "Deleted {$count} unassigned " . Str :: plural ( 'ability' , $ count ) . '.' ) ; } else { $ this -> info ( 'No unassigned abilities.' ) ; } }
Delete abilities not assigned to anyone .
3,865
protected function getUnassignedAbilitiesQuery ( ) { $ model = Models :: ability ( ) ; return $ model -> whereNotIn ( $ model -> getKeyName ( ) , function ( $ query ) { $ query -> from ( Models :: table ( 'permissions' ) ) -> select ( 'ability_id' ) ; } ) ; }
Get the base query for all unassigned abilities .
3,866
protected function deleteOrphanedAbilities ( ) { $ query = $ this -> getBaseOrphanedQuery ( ) -> where ( function ( $ query ) { foreach ( $ this -> getEntityModels ( ) as $ modelName ) { $ query -> orWhere ( function ( $ query ) use ( $ modelName ) { $ this -> scopeQueryToWhereModelIsMissing ( $ query , $ modelName ) ; } ) ; } } ) ; if ( ( $ count = $ query -> count ( ) ) > 0 ) { $ query -> delete ( ) ; $ this -> info ( "Deleted {$count} orphaned " . Str :: plural ( 'ability' , $ count ) . '.' ) ; } else { $ this -> info ( 'No orphaned abilities.' ) ; } }
Delete model abilities whose models have been deleted .
3,867
protected function scopeQueryToWhereModelIsMissing ( $ query , $ modelName ) { $ model = new $ modelName ; $ table = $ this -> abilitiesTable ( ) ; $ query -> where ( "{$table}.entity_type" , $ modelName ) ; $ query -> whereNotIn ( "{$table}.entity_id" , function ( $ query ) use ( $ modelName ) { $ model = new $ modelName ; $ table = $ model -> getTable ( ) ; $ query -> from ( $ table ) -> select ( $ table . '.' . $ model -> getKeyName ( ) ) ; } ) ; }
Scope the given query to where the ability s model is missing .
3,868
public function to ( $ abilities , $ model = null , array $ attributes = [ ] ) { if ( call_user_func_array ( [ $ this , 'shouldConductLazy' ] , func_get_args ( ) ) ) { return $ this -> conductLazy ( $ abilities ) ; } $ ids = $ this -> getAbilityIds ( $ abilities , $ model , $ attributes ) ; $ this -> giveAbilities ( $ ids , $ this -> getAuthority ( ) ) ; }
Give the abilities to the authority .
3,869
protected function giveAbilities ( array $ ids , Model $ authority = null ) { $ ids = array_diff ( $ ids , $ this -> getAssociatedAbilityIds ( $ authority , $ ids , false ) ) ; if ( is_null ( $ authority ) ) { $ this -> giveAbilitiesToEveryone ( $ ids ) ; } else { $ this -> giveAbilitiesToAuthority ( $ ids , $ authority ) ; } }
Associate the given ability IDs as allowed abilities .
3,870
protected function giveAbilitiesToAuthority ( array $ ids , Model $ authority ) { $ attributes = Models :: scope ( ) -> getAttachAttributes ( get_class ( $ authority ) ) ; $ authority -> abilities ( ) -> attach ( $ ids , $ attributes ) ; }
Grant permission to these abilities to the given authority .
3,871
protected function giveAbilitiesToEveryone ( array $ ids ) { $ attributes = Models :: scope ( ) -> getAttachAttributes ( ) ; $ records = array_map ( function ( $ id ) use ( $ attributes ) { return [ 'ability_id' => $ id ] + $ attributes ; } , $ ids ) ; Models :: query ( 'permissions' ) -> insert ( $ records ) ; }
Grant everyone permission to the given abilities .
3,872
public function handle ( $ request , Closure $ next ) { $ tenantId = $ request -> user ( ) -> account_id ; $ this -> bouncer -> scope ( ) -> to ( $ tenantId ) ; return $ next ( $ request ) ; }
Set the proper Bouncer scope for the incoming request .
3,873
public static function scope ( ScopeContract $ scope = null ) { if ( ! is_null ( $ scope ) ) { return static :: $ scope = $ scope ; } if ( is_null ( static :: $ scope ) ) { static :: $ scope = new Scope ; } return static :: $ scope ; }
Get or set the model scoping instance .
3,874
public static function updateMorphMap ( $ classNames = null ) { if ( is_null ( $ classNames ) ) { $ classNames = [ static :: classname ( Role :: class ) , static :: classname ( Ability :: class ) , ] ; } Relation :: morphMap ( $ classNames ) ; }
Update Eloquent s morph map with the Bouncer models and tables .
3,875
public static function isOwnedBy ( Model $ authority , Model $ model ) { $ type = get_class ( $ model ) ; if ( isset ( static :: $ ownership [ $ type ] ) ) { $ attribute = static :: $ ownership [ $ type ] ; } elseif ( isset ( static :: $ ownership [ '*' ] ) ) { $ attribute = static :: $ ownership [ '*' ] ; } else { $ attribute = strtolower ( static :: basename ( $ authority ) ) . '_id' ; } return static :: isOwnedVia ( $ attribute , $ authority , $ model ) ; }
Determines whether the given model is owned by the given authority .
3,876
protected static function isOwnedVia ( $ attribute , Model $ authority , Model $ model ) { if ( $ attribute instanceof Closure ) { return $ attribute ( $ model , $ authority ) ; } return $ authority -> getKey ( ) == $ model -> { $ attribute } ; }
Determines ownership via the given attribute .
3,877
public static function query ( $ table ) { $ query = new Builder ( $ connection = static :: user ( ) -> getConnection ( ) , $ connection -> getQueryGrammar ( ) , $ connection -> getPostProcessor ( ) ) ; return $ query -> from ( static :: table ( $ table ) ) ; }
Get a new query builder instance .
3,878
public function to ( $ ability , array $ attributes = [ ] ) { $ this -> ability = $ ability ; $ this -> attributes = array_merge ( $ this -> attributes , $ attributes ) ; }
Limit ownership to the given ability .
3,879
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 ) ; }
Get the IDs of the provided abilities .
3,880
protected function getAbilityIdsFromMap ( array $ map , array $ attributes ) { list ( $ map , $ list ) = Helpers :: partition ( $ map , function ( $ value , $ key ) { return ! is_int ( $ key ) ; } ) ; return $ map -> map ( function ( $ entity , $ ability ) use ( $ attributes ) { return $ this -> getAbilityIds ( $ ability , $ entity , $ attributes ) ; } ) -> collapse ( ) -> merge ( $ this -> getAbilityIdsFromArray ( $ list , $ attributes ) ) -> all ( ) ; }
Get the ability IDs for the given map .
3,881
protected function getAbilityIdsFromArray ( $ abilities , array $ attributes ) { $ groups = Helpers :: groupModelsAndIdentifiersByType ( $ abilities ) ; $ keyName = Models :: ability ( ) -> getKeyName ( ) ; $ groups [ 'strings' ] = $ this -> abilitiesByName ( $ groups [ 'strings' ] , $ attributes ) -> pluck ( $ keyName ) -> all ( ) ; $ groups [ 'models' ] = Arr :: pluck ( $ groups [ 'models' ] , $ keyName ) ; return Arr :: collapse ( $ groups ) ; }
Get the ability IDs from the provided array creating the ones that don t exist .
3,882
protected function getModelAbilityKeys ( $ abilities , $ model , array $ attributes ) { $ abilities = Collection :: make ( is_array ( $ abilities ) ? $ abilities : [ $ abilities ] ) ; $ models = Collection :: make ( is_array ( $ model ) ? $ model : [ $ model ] ) ; return $ abilities -> map ( function ( $ ability ) use ( $ models , $ attributes ) { return $ models -> map ( function ( $ model ) use ( $ ability , $ attributes ) { return $ this -> getModelAbility ( $ ability , $ model , $ attributes ) -> getKey ( ) ; } ) ; } ) -> collapse ( ) -> all ( ) ; }
Get the abilities for the given model ability descriptors .
3,883
protected function getModelAbility ( $ ability , $ entity , array $ attributes ) { $ entity = $ this -> getEntityInstance ( $ entity ) ; $ existing = $ this -> findAbility ( $ ability , $ entity , $ attributes ) ; return $ existing ? : $ this -> createAbility ( $ ability , $ entity , $ attributes ) ; }
Get an ability for the given entity .
3,884
protected function findAbility ( $ ability , $ entity , $ attributes ) { $ onlyOwned = isset ( $ attributes [ 'only_owned' ] ) ? $ attributes [ 'only_owned' ] : false ; $ query = Models :: ability ( ) -> where ( 'name' , $ ability ) -> forModel ( $ entity , true ) -> where ( 'only_owned' , $ onlyOwned ) ; return Models :: scope ( ) -> applyToModelQuery ( $ query ) -> first ( ) ; }
Find the ability for the given entity .
3,885
protected function createAbility ( $ ability , $ entity , $ attributes ) { return Models :: ability ( ) -> createForModel ( $ entity , $ attributes + [ 'name' => $ ability , ] ) ; }
Create an ability for the given entity .
3,886
protected function getEntityInstance ( $ model ) { if ( $ model === '*' ) { return '*' ; } if ( ! $ model instanceof Model ) { return new $ model ; } if ( ! $ model -> exists ) { throw new InvalidArgumentException ( 'The model does not exist. To edit access to all models, use the class name instead' ) ; } return $ model ; }
Get an instance of the given model .
3,887
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 ) ) ; }
Get or create abilities by their name .
3,888
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 ) ; }
Create the non - existant abilities by name .
3,889
public function create ( ) { $ gate = $ this -> getGate ( ) ; $ guard = $ this -> getGuard ( ) ; $ bouncer = ( new Bouncer ( $ guard ) ) -> setGate ( $ gate ) ; if ( $ this -> registerAtGate ) { $ guard -> registerAt ( $ gate ) ; } if ( $ this -> registerAtContainer ) { $ bouncer -> registerClipboardAtContainer ( ) ; } return $ bouncer ; }
Create an instance of Bouncer .
3,890
protected function getGate ( ) { if ( $ this -> gate ) { return $ this -> gate ; } return new Gate ( $ this -> getContainer ( ) , function ( ) { return $ this -> user ; } ) ; }
Get an instance of the gate .
3,891
public function constrain ( $ query , $ model , $ strict = false ) { if ( $ model === '*' ) { return $ this -> constrainByWildcard ( $ query ) ; } $ model = is_string ( $ model ) ? new $ model : $ model ; $ this -> constrainByModel ( $ query , $ model , $ strict ) ; }
Constrain a query to an ability for a specific model or wildcard .
3,892
protected function modelAbilityConstraint ( Model $ model , $ strict ) { return function ( $ query ) use ( $ model , $ strict ) { $ query -> where ( "{$this->table}.entity_type" , $ model -> getMorphClass ( ) ) ; $ query -> where ( $ this -> abilitySubqueryConstraint ( $ model , $ strict ) ) ; } ; }
Get the constraint for regular model abilities .
3,893
protected function abilitySubqueryConstraint ( Model $ model , $ strict ) { return function ( $ query ) use ( $ model , $ strict ) { if ( ! $ model -> exists || ! $ strict ) { $ query -> whereNull ( "{$this->table}.entity_id" ) ; } if ( $ model -> exists ) { $ query -> orWhere ( "{$this->table}.entity_id" , $ model -> getKey ( ) ) ; } } ; }
Get the constraint for the ability subquery .
3,894
public static function register ( $ model ) { if ( interface_exists ( EloquentScope :: class ) ) { $ model :: addGlobalScope ( new TenantScope ) ; } else { $ model :: addGlobalScope ( new LegacyTenantScope ) ; } }
Register the correct global scope class .
3,895
protected function forbidAbilities ( array $ ids , Model $ authority = null ) { $ ids = array_diff ( $ ids , $ this -> getAssociatedAbilityIds ( $ authority , $ ids , true ) ) ; if ( is_null ( $ authority ) ) { $ this -> forbidAbilitiesToEveryone ( $ ids ) ; } else { $ this -> forbidAbilitiesToAuthority ( $ ids , $ authority ) ; } }
Associate the given abilitiy IDs as forbidden abilities .
3,896
protected function forbidAbilitiesToAuthority ( array $ ids , Model $ authority ) { $ attributes = Models :: scope ( ) -> getAttachAttributes ( get_class ( $ authority ) ) ; $ authority -> abilities ( ) -> attach ( $ ids , $ attributes + [ 'forbidden' => true ] ) ; }
Forbid these abilities to the given authority .
3,897
protected function forbidAbilitiesToEveryone ( array $ ids ) { $ attributes = Models :: scope ( ) -> getAttachAttributes ( ) + [ 'forbidden' => true ] ; $ records = array_map ( function ( $ id ) use ( $ attributes ) { return [ 'ability_id' => $ id ] + $ attributes ; } , $ ids ) ; Models :: query ( 'permissions' ) -> insert ( $ records ) ; }
Forbid the given abilities for everyone .
3,898
public function getGrant ( $ name ) { if ( empty ( $ this -> registry [ $ name ] ) ) { $ this -> registerDefaultGrant ( $ name ) ; } return $ this -> registry [ $ name ] ; }
Returns a grant singleton by name .
3,899
protected function registerDefaultGrant ( $ name ) { $ class = str_replace ( ' ' , '' , ucwords ( str_replace ( [ '-' , '_' ] , ' ' , $ name ) ) ) ; $ class = 'League\\OAuth2\\Client\\Grant\\' . $ class ; $ this -> checkGrant ( $ class ) ; return $ this -> setGrant ( $ name , new $ class ) ; }
Registers a default grant singleton by name .