1 <?php
 2 
 3 /**
 4  * Class for permissions.
 5  *
 6  * @author gharlan
 7  *
 8  * @package redaxo\core\login
 9  */
10 abstract class rex_perm
11 {
12     const GENERAL = 'general';
13     const OPTIONS = 'options';
14     const EXTRAS = 'extras';
15 
16     /**
17      * Array of permissions.
18      *
19      * @var array
20      */
21     private static $perms = [];
22 
23     /**
24      * Registers a new permission.
25      *
26      * @param string $perm  Perm key
27      * @param string $name  Perm name
28      * @param string $group Perm group, possible values are rex_perm::GENERAL, rex_perm::OPTIONS and rex_perm::EXTRAS
29      */
30     public static function register($perm, $name = null, $group = self::GENERAL)
31     {
32         $name = $name ?: (rex_i18n::hasMsg($key = 'perm_' . $group . '_' . $perm) ? rex_i18n::rawMsg($key) : $perm);
33         self::$perms[$group][$perm] = $name;
34     }
35 
36     /**
37      * Returns whether the permission is registered.
38      *
39      * @param string $perm
40      *
41      * @return bool
42      */
43     public static function has($perm)
44     {
45         foreach (self::$perms as $perms) {
46             if (isset($perms[$perm])) {
47                 return true;
48             }
49         }
50         return false;
51     }
52 
53     /**
54      * Returns all permissions for the given group.
55      *
56      * @param string $group Perm group
57      *
58      * @return array Permissions
59      */
60     public static function getAll($group = self::GENERAL)
61     {
62         if (isset(self::$perms[$group])) {
63             $perms = self::$perms[$group];
64             asort($perms);
65             return $perms;
66         }
67         return [];
68     }
69 }
70