| 1 | <?php |
|---|
| 2 | // WCF include |
|---|
| 3 | require_once(WCF_DIR.'lib/system/event/EventListener.class.php'); |
|---|
| 4 | require_once(WCF_DIR.'lib/system/database/ConditionBuilder.class.php'); |
|---|
| 5 | |
|---|
| 6 | /** |
|---|
| 7 | * removes useroptions not allowed or overwrites the value with a message |
|---|
| 8 | * |
|---|
| 9 | * @author Torben Brodt |
|---|
| 10 | * @license GNU General Public License <http://opensource.org/licenses/gpl-3.0.html> |
|---|
| 11 | * @package de.easy-coding.wcf.profileaccessmatrix |
|---|
| 12 | */ |
|---|
| 13 | class ProfileAccessMatrixUserSearchFormListener implements EventListener { |
|---|
| 14 | protected $eventObj; |
|---|
| 15 | protected $className; |
|---|
| 16 | |
|---|
| 17 | /** |
|---|
| 18 | * @see EventListener::execute() |
|---|
| 19 | */ |
|---|
| 20 | public function execute($eventObj, $className, $eventName) { |
|---|
| 21 | $this->eventObj = $eventObj; |
|---|
| 22 | $this->className = $className; |
|---|
| 23 | |
|---|
| 24 | switch ($eventName) { |
|---|
| 25 | case 'buildConditions': |
|---|
| 26 | $this->buildConditions($eventObj->conditions); |
|---|
| 27 | break; |
|---|
| 28 | } |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | /** |
|---|
| 32 | * all groups which the user can access |
|---|
| 33 | * @return boolean true if user has permission |
|---|
| 34 | */ |
|---|
| 35 | protected function accessibleGroups($matrix) { |
|---|
| 36 | $accessibleGroups = array(); |
|---|
| 37 | foreach($matrix as $owner => $arr) { |
|---|
| 38 | foreach(WCF::getUser()->getGroupIDs() as $viewer) { |
|---|
| 39 | if(isset($matrix[$owner]) && in_array($viewer, $matrix[$owner])) { |
|---|
| 40 | $accessibleGroups[] = $owner; |
|---|
| 41 | break; |
|---|
| 42 | } |
|---|
| 43 | } |
|---|
| 44 | } |
|---|
| 45 | return $accessibleGroups; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | * Returns an object of the requested option type. |
|---|
| 50 | * |
|---|
| 51 | * @param string $type |
|---|
| 52 | * @return OptionType |
|---|
| 53 | */ |
|---|
| 54 | protected function getTypeObject($type) { |
|---|
| 55 | if (!isset($this->typeObjects[$type])) { |
|---|
| 56 | $className = 'OptionType'.ucfirst(strtolower($type)); |
|---|
| 57 | $classPath = WCF_DIR.'lib/acp/option/'.$className.'.class.php'; |
|---|
| 58 | |
|---|
| 59 | // include class file |
|---|
| 60 | if (!file_exists($classPath)) { |
|---|
| 61 | throw new SystemException("unable to find class file '".$classPath."'", 11000); |
|---|
| 62 | } |
|---|
| 63 | require_once($classPath); |
|---|
| 64 | |
|---|
| 65 | // create instance |
|---|
| 66 | if (!class_exists($className)) { |
|---|
| 67 | throw new SystemException("unable to find class '".$className."'", 11001); |
|---|
| 68 | } |
|---|
| 69 | $this->typeObjects[$type] = new $className(); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | return $this->typeObjects[$type]; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | * |
|---|
| 77 | * @param conditions |
|---|
| 78 | */ |
|---|
| 79 | protected function buildConditions(ConditionBuilder $conditions) { |
|---|
| 80 | // load cache |
|---|
| 81 | WCF::getCache()->addResource('profileaccessmatrix', |
|---|
| 82 | WCF_DIR.'cache/cache.profileaccessmatrix.php', |
|---|
| 83 | WCF_DIR.'lib/system/cache/CacheBuilderProfileAccessMatrix.class.php'); |
|---|
| 84 | |
|---|
| 85 | $matrix = WCF::getCache()->get('profileaccessmatrix'); |
|---|
| 86 | $check = $this->accessibleGroups($matrix); |
|---|
| 87 | |
|---|
| 88 | foreach ($this->eventObj->activeOptions as $name => $option) { |
|---|
| 89 | if(isset($this->eventObj->values[$option['optionName']])) { |
|---|
| 90 | $value = $this->eventObj->values[$option['optionName']]; |
|---|
| 91 | $condition = $this->getTypeObject($option['optionType'])->getCondition($option, $value, isset($this->matchExactly[$name])); |
|---|
| 92 | if ($condition !== false) { |
|---|
| 93 | if(count($check)>0) { |
|---|
| 94 | $conditions->add("($condition AND ( |
|---|
| 95 | SELECT COUNT(*) |
|---|
| 96 | FROM wcf".WCF_N."_user_to_groups ug |
|---|
| 97 | WHERE ug.userID = user.userID |
|---|
| 98 | AND ug.groupID IN (".implode(',',$check).") |
|---|
| 99 | ) > 0)"); |
|---|
| 100 | } else { |
|---|
| 101 | $conditions->add("($condition AND 0)"); |
|---|
| 102 | |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | ?> |
|---|