| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* What a connection is allowed to do. |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Extendify\Mcp; |
| 8 |
|
| 9 |
defined('ABSPATH') || die('No direct access.'); |
| 10 |
|
| 11 |
/** |
| 12 |
* The names a connection row stores. |
| 13 |
*/ |
| 14 |
class Grants |
| 15 |
{ |
| 16 |
// phpcs:disable PSR12.Properties.ConstantVisibility.NotFound |
| 17 |
const READ = 'read'; |
| 18 |
|
| 19 |
const WRITE = 'write'; |
| 20 |
// phpcs:enable PSR12.Properties.ConstantVisibility.NotFound |
| 21 |
|
| 22 |
/** |
| 23 |
* @return array |
| 24 |
*/ |
| 25 |
public static function names() |
| 26 |
{ |
| 27 |
return [self::READ, self::WRITE]; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* A row storing no grants could only ever read. |
| 32 |
* |
| 33 |
* @param mixed $grants - Whatever the connection stored. |
| 34 |
* @return array |
| 35 |
*/ |
| 36 |
public static function sanitize($grants) |
| 37 |
{ |
| 38 |
$known = array_values(array_intersect(self::names(), is_array($grants) ? $grants : [])); |
| 39 |
|
| 40 |
return $known ?: [self::READ]; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* @param string $scope - The space-separated scope an OAuth client asked for. |
| 45 |
* @return array |
| 46 |
*/ |
| 47 |
public static function fromScope($scope) |
| 48 |
{ |
| 49 |
$asked = preg_split('/\s+/', trim((string) $scope)); |
| 50 |
|
| 51 |
return in_array(self::WRITE, $asked, true) ? self::names() : [self::READ]; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @param array $grants - The connection's grants. |
| 56 |
* @return string |
| 57 |
*/ |
| 58 |
public static function label(array $grants) |
| 59 |
{ |
| 60 |
return in_array(self::WRITE, $grants, true) |
| 61 |
/* translators: an access level, shown on a badge and on the approval page; a noun phrase, not a command. */ |
| 62 |
? \__('Read and write', 'extendify-local') |
| 63 |
/* translators: an access level, shown on a badge and on the approval page; a noun phrase, not a command. */ |
| 64 |
: \__('Read only', 'extendify-local'); |
| 65 |
} |
| 66 |
} |
| 67 |
|