Menu

## AuthTrait

AuthTrait.php : 16

This trait provides common authentication methods for the services

It is used to check if the user is authenticated, has the required permissions, and other authentication-related methods

##### Tags

author

  Jet - ezrajet9@gmail.com

### Table of Contents

#### Methods

auth() : ContextUserObject|null
  This method holds the currently logged in user object
authExtraHas() : bool
  Checks if the auth extra data contains a key or not
can() : bool
  Like CanAny but only check for one permission at a time
canAll() : bool
  Similar to canAny only that this checks if the user has all the passed permissions
canAny() : bool
  This method is used to check if the currently logged in user has the required permission to access a resource.
getAuthExtraByKey() : mixed|null
  Returns the auth extra data by key
mustAuthenticate() : bool
  This method ensures that only authenticated users can access a resource

### Methods

#### auth()

AuthTrait.php : 126

This method holds the currently logged in user object

`public auth() : ContextUserObject|null`

##### Return values

ContextUserObject|null —

The currently logged in user object or null if no user is logged in

#### authExtraHas()

AuthTrait.php : 152

Checks if the auth extra data contains a key or not

`public authExtraHas(string $key) : bool`

##### Parameters

$key : string

##### Return values

bool —

Returns true if the key is present in the authExtra data else returns false

#### can()

AuthTrait.php : 73

Like CanAny but only check for one permission at a time

`public can(string $permission[, string|null $message = 'You do not have access to this resource' ]) : bool`

##### Parameters

$permission : string

  The permission to check for

$message : string|null = 'You do not have access to this resource'

  The message to be returned if the user does not have the required permission

##### Tags

throws
  UserUnauthenticatedException
throws
  UserUnauthorizedException

##### Return values

bool —

Returns true if the user has the required permission

#### canAll()

AuthTrait.php : 100

Similar to canAny only that this checks if the user has all the passed permissions

`public canAll(array<string|int, mixed> $permissions[, string|null $message = 'You do not have access to this resource' ]) : bool`

##### Parameters

$permissions : array<string|int, mixed>

  The permissions to check for

$message : string|null = 'You do not have access to this resource'

  The message to be returned if the user does not have the required permission

##### Tags

throws
  UserUnauthenticatedException

  If the user is not authenticated

throws
  UserUnauthorizedException

  If the user does not have the required permission

##### Return values

bool —

Returns true if the user has the required permission, else returns a BaseResponse object

#### canAny()

AuthTrait.php : 33

This method is used to check if the currently logged in user has the required permission to access a resource.

`public canAny(string|array<string|int, mixed> $permission[, string|null $message = 'You do not have access to this resource' ]) : bool`

It checks if user is logged in first, then checks if there are any permissions set for the user.

And finally checks if the set permissions contain the given permission we are looking for.

##### Parameters

$permission : string|array<string|int, mixed>

  The permission to check for

$message : string|null = 'You do not have access to this resource'

  The message to be returned if the user does not have the required permission

##### Tags

throws
  UserUnauthenticatedException
throws
  UserUnauthorizedException

##### Return values

bool —

Returns true if the user has the required permission

#### getAuthExtraByKey()

AuthTrait.php : 167

Returns the auth extra data by key

`public getAuthExtraByKey(string $key) : mixed|null`

##### Parameters

$key : string

##### Return values

mixed|null —

Returns the value of the key if it exists else returns null

#### mustAuthenticate()

AuthTrait.php : 138

This method ensures that only authenticated users can access a resource

`public mustAuthenticate([string|null $message = 'You must be authenticated to access this resource' ]) : bool`

##### Parameters

$message : string|null = 'You must be authenticated to access this resource'

  Use this to override the default message

##### Tags

throws
  UserUnauthenticatedException

##### Return values

bool

## Search results