Cache.php
7 years ago
Capability.php
7 years ago
LoginRedirect.php
7 years ago
LogoutRedirect.php
7 years ago
Menu.php
7 years ago
Metabox.php
7 years ago
Policy.php
7 years ago
Post.php
7 years ago
Redirect.php
7 years ago
Route.php
7 years ago
Toolbar.php
7 years ago
Uri.php
7 years ago
Visibility.php
7 years ago
Capability.php
116 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * ====================================================================== |
| 5 | * LICENSE: This file is subject to the terms and conditions defined in * |
| 6 | * file 'license.txt', which is part of this source code package. * |
| 7 | * ====================================================================== |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Capability object |
| 12 | * |
| 13 | * @package AAM |
| 14 | * @author Vasyl Martyniuk <vasyl@vasyltech.com> |
| 15 | */ |
| 16 | class AAM_Core_Object_Capability extends AAM_Core_Object { |
| 17 | |
| 18 | /** |
| 19 | * Constructor |
| 20 | * |
| 21 | * @param AAM_Core_Subject $subject |
| 22 | * |
| 23 | * @return void |
| 24 | * |
| 25 | * @access public |
| 26 | */ |
| 27 | public function __construct(AAM_Core_Subject $subject) { |
| 28 | parent::__construct($subject); |
| 29 | |
| 30 | $caps = $this->getSubject()->getCapabilities(); |
| 31 | |
| 32 | // Load Capabilities from the policy |
| 33 | $stms = AAM_Core_Policy_Manager::getInstance()->find( |
| 34 | "/^Capability:/i", $subject |
| 35 | ); |
| 36 | |
| 37 | foreach($stms as $key => $stm) { |
| 38 | $chunks = explode(':', $key); |
| 39 | if (count($chunks) === 2) { |
| 40 | $caps[$chunks[1]] = ($stm['Effect'] === 'allow' ? 1 : 0); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | //check if capabilities are overwritten but only for user subject |
| 45 | if (is_a($this->getSubject(), 'AAM_Core_Subject_User')) { |
| 46 | $userCaps = get_user_option( |
| 47 | AAM_Core_Subject_User::AAM_CAPKEY, $this->getSubject()->getId() |
| 48 | ); |
| 49 | if (!empty($userCaps)) { |
| 50 | $caps = array_merge($caps, $userCaps); |
| 51 | $this->setOverwritten(true); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | $this->setOption($caps); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Update subject's capability |
| 60 | * |
| 61 | * @param string $capability |
| 62 | * @param bool $granted |
| 63 | * |
| 64 | * @return bool |
| 65 | * |
| 66 | * @access public |
| 67 | */ |
| 68 | public function save($capability, $granted) { |
| 69 | if (intval($granted)) { |
| 70 | $result = $this->getSubject()->addCapability($capability); |
| 71 | } else { |
| 72 | $result = $this->getSubject()->removeCapability($capability); |
| 73 | } |
| 74 | |
| 75 | return $result; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Check if subject has specified capability |
| 80 | * |
| 81 | * @param string $capability |
| 82 | * |
| 83 | * @return bool |
| 84 | * |
| 85 | * @access public |
| 86 | */ |
| 87 | public function has($capability) { |
| 88 | return $this->getSubject()->hasCapability($capability); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Assign capability to user |
| 93 | * |
| 94 | * @param string $capability |
| 95 | * |
| 96 | * @return boolean |
| 97 | * |
| 98 | * @access public |
| 99 | */ |
| 100 | public function add($capability) { |
| 101 | return $this->save($capability, 1); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Remove capability from user |
| 106 | * |
| 107 | * @param string $capability |
| 108 | * |
| 109 | * @return boolean |
| 110 | * |
| 111 | * @access public |
| 112 | */ |
| 113 | public function remove($capability) { |
| 114 | return $this->save($capability, 0); |
| 115 | } |
| 116 | } |