MediaWiki  master
SimpleAuthority.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Permissions;
22 
23 use InvalidArgumentException;
27 
37 class SimpleAuthority implements Authority {
38 
40  private $actor;
41 
43  private $permissions;
44 
50  public function __construct( UserIdentity $actor, array $permissions ) {
51  $this->actor = $actor;
52  $this->permissions = array_fill_keys( $permissions, true );
53  }
54 
60  public function getUser(): UserIdentity {
61  return $this->actor;
62  }
63 
70  public function getBlock( int $freshness = self::READ_NORMAL ): ?Block {
71  return null;
72  }
73 
81  public function isAllowed( string $permission ): bool {
82  return isset( $this->permissions[ $permission ] );
83  }
84 
92  public function isAllowedAny( ...$permissions ): bool {
93  if ( !$permissions ) {
94  throw new InvalidArgumentException( 'At least one permission must be specified' );
95  }
96 
97  foreach ( $permissions as $perm ) {
98  if ( $this->isAllowed( $perm ) ) {
99  return true;
100  }
101  }
102 
103  return false;
104  }
105 
113  public function isAllowedAll( ...$permissions ): bool {
114  if ( !$permissions ) {
115  throw new InvalidArgumentException( 'At least one permission must be specified' );
116  }
117 
118  foreach ( $permissions as $perm ) {
119  if ( !$this->isAllowed( $perm ) ) {
120  return false;
121  }
122  }
123 
124  return true;
125  }
126 
127  private function checkPermission( string $permission, ?PermissionStatus $status ): bool {
128  $ok = $this->isAllowed( $permission );
129 
130  if ( !$ok && $status ) {
131  // TODO: use a message that at includes the permission name
132  $status->fatal( 'permissionserrors' );
133  }
134 
135  return $ok;
136  }
137 
147  public function probablyCan(
148  string $action,
149  PageIdentity $target,
150  PermissionStatus $status = null
151  ): bool {
152  return $this->checkPermission( $action, $status );
153  }
154 
164  public function definitelyCan(
165  string $action,
166  PageIdentity $target,
167  PermissionStatus $status = null
168  ): bool {
169  return $this->checkPermission( $action, $status );
170  }
171 
181  public function authorizeRead(
182  string $action,
183  PageIdentity $target,
184  PermissionStatus $status = null
185  ): bool {
186  return $this->checkPermission( $action, $status );
187  }
188 
198  public function authorizeWrite(
199  string $action,
200  PageIdentity $target,
201  PermissionStatus $status = null
202  ): bool {
203  return $this->checkPermission( $action, $status );
204  }
205 
206 }
Page\PageIdentity
Interface for objects (potentially) representing an editable wiki page.
Definition: PageIdentity.php:64
MediaWiki\Permissions\SimpleAuthority
Represents an authority that has a specific set of permissions which are specified explicitly.
Definition: SimpleAuthority.php:37
MediaWiki\Permissions\SimpleAuthority\isAllowed
isAllowed(string $permission)
Checks whether this authority has the given permission in general.For some permissions,...
Definition: SimpleAuthority.php:81
MediaWiki\Permissions\SimpleAuthority\$permissions
true[] $permissions
permissions (stored in the keys, values are ignored)
Definition: SimpleAuthority.php:43
StatusValue\fatal
fatal( $message,... $parameters)
Add an error and set OK to false, indicating that the operation as a whole was fatal.
Definition: StatusValue.php:266
MediaWiki\Permissions\SimpleAuthority\definitelyCan
definitelyCan(string $action, PageIdentity $target, PermissionStatus $status=null)
Checks whether this authority can perform the given action on the given target page....
Definition: SimpleAuthority.php:164
MediaWiki\Permissions\SimpleAuthority\getUser
getUser()
The user identity associated with this authority.
Definition: SimpleAuthority.php:60
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
MediaWiki\Permissions\SimpleAuthority\__construct
__construct(UserIdentity $actor, array $permissions)
Definition: SimpleAuthority.php:50
MediaWiki\Permissions\SimpleAuthority\isAllowedAll
isAllowedAll(... $permissions)
Checks whether this authority has any of the given permissions in general.Implementations must ensure...
Definition: SimpleAuthority.php:113
MediaWiki\Permissions\SimpleAuthority\$actor
UserIdentity $actor
Definition: SimpleAuthority.php:40
MediaWiki\Block\Block
Represents a block that may prevent users from performing specific operations.
Definition: Block.php:40
MediaWiki\Permissions\Authority
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
MediaWiki\Permissions\SimpleAuthority\isAllowedAny
isAllowedAny(... $permissions)
Checks whether this authority has any of the given permissions in general.Implementations must ensure...
Definition: SimpleAuthority.php:92
MediaWiki\Permissions\PermissionStatus
A StatusValue for permission errors.
Definition: PermissionStatus.php:35
MediaWiki\$action
string $action
Cache what action this request is.
Definition: MediaWiki.php:45
MediaWiki\Permissions\SimpleAuthority\authorizeRead
authorizeRead(string $action, PageIdentity $target, PermissionStatus $status=null)
Authorize read access.This should be used immediately before performing read access on restricted inf...
Definition: SimpleAuthority.php:181
MediaWiki\Permissions\SimpleAuthority\authorizeWrite
authorizeWrite(string $action, PageIdentity $target, PermissionStatus $status=null)
Authorize write access.This should be used immediately before updating persisted information....
Definition: SimpleAuthority.php:198
MediaWiki\Permissions\SimpleAuthority\probablyCan
probablyCan(string $action, PageIdentity $target, PermissionStatus $status=null)
Checks whether this authority can probably perform the given action on the given target page....
Definition: SimpleAuthority.php:147
MediaWiki\Permissions\SimpleAuthority\getBlock
getBlock(int $freshness=self::READ_NORMAL)
Definition: SimpleAuthority.php:70
MediaWiki\Permissions\SimpleAuthority\checkPermission
checkPermission(string $permission, ?PermissionStatus $status)
Definition: SimpleAuthority.php:127
MediaWiki\Permissions
Definition: Authority.php:21