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
Post.php
7 years ago
Redirect.php
7 years ago
Route.php
7 years ago
Toolbar.php
7 years ago
Visibility.php
7 years ago
Capability.php
101 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 | $this->setOption($this->getSubject()->getCapabilities()); |
| 31 | |
| 32 | //check if capabilities are overwritten but only for user subject |
| 33 | if (is_a($this->getSubject(), 'AAM_Core_Subject_User')) { |
| 34 | $caps = get_user_option( |
| 35 | AAM_Core_Subject_User::AAM_CAPKEY, $this->getSubject()->getId() |
| 36 | ); |
| 37 | if (!empty($caps)) { |
| 38 | $this->setOverwritten(true); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Update subject's capability |
| 45 | * |
| 46 | * @param string $capability |
| 47 | * @param bool $granted |
| 48 | * |
| 49 | * @return bool |
| 50 | * |
| 51 | * @access public |
| 52 | */ |
| 53 | public function save($capability, $granted) { |
| 54 | if (intval($granted)) { |
| 55 | $result = $this->getSubject()->addCapability($capability); |
| 56 | } else { |
| 57 | $result = $this->getSubject()->removeCapability($capability); |
| 58 | } |
| 59 | |
| 60 | return $result; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Check if subject has specified capability |
| 65 | * |
| 66 | * @param string $capability |
| 67 | * |
| 68 | * @return bool |
| 69 | * |
| 70 | * @access public |
| 71 | */ |
| 72 | public function has($capability) { |
| 73 | return $this->getSubject()->hasCapability($capability); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Assign capability to user |
| 78 | * |
| 79 | * @param string $capability |
| 80 | * |
| 81 | * @return boolean |
| 82 | * |
| 83 | * @access public |
| 84 | */ |
| 85 | public function add($capability) { |
| 86 | return $this->save($capability, 1); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Remove capability from user |
| 91 | * |
| 92 | * @param string $capability |
| 93 | * |
| 94 | * @return boolean |
| 95 | * |
| 96 | * @access public |
| 97 | */ |
| 98 | public function remove($capability) { |
| 99 | return $this->save($capability, 0); |
| 100 | } |
| 101 | } |