예제 #1
0
 public function getTreePermissionsAction()
 {
     $this->removeViewRenderer();
     $user = User::getById($this->_getParam("user"));
     if ($this->_getParam("xaction") == "update") {
         $data = json_decode($this->_getParam("data"));
         if (!empty($data->id)) {
             $nodes[] = $data;
         } else {
             $nodes = $data;
         }
         //loop through store nodes  = assets to edit
         if (is_array($nodes)) {
             foreach ($nodes as $node) {
                 $asset = Asset::getById($node->id);
                 $parent = Asset::getById($asset->getParentId());
                 $assetPermission = $asset->getPermissionsForUser($user);
                 if ($assetPermission instanceof Asset_Permissions) {
                     $found = true;
                     if (!$node->permissionSet) {
                         //reset permission by deleting it
                         if ($assetPermission->getCid() == $asset->getId()) {
                             $assetPermission->delete();
                             $permissions = $asset->getPermissions();
                         }
                         break;
                     } else {
                         if ($assetPermission->getCid() != $asset->getId() or $assetPermission->getUser()->getId() != $user->getId()) {
                             //we got a parent's permission create new permission
                             //or we got a usergroup permission, create a new permission for specific user
                             $assetPermission = new Asset_Permissions();
                             $assetPermission->setUser($user);
                             $assetPermission->setUserId($user->getId());
                             $assetPermission->setUsername($user->getUsername());
                             $assetPermission->setCid($asset->getId());
                             $assetPermission->setCpath($asset->getFullPath());
                         }
                         //update asset_permissions
                         $doSave = true;
                         $permissionNames = $assetPermission->getValidPermissionKeys();
                         foreach ($permissionNames as $name) {
                             //check if parent allows list
                             if ($parent) {
                                 $parent->getPermissionsForUser($user);
                                 $parentList = $parent->isAllowed("list");
                             } else {
                                 $parentList = true;
                             }
                             $setterName = "set" . ucfirst($name);
                             if (isset($node->{$name}) and $node->{$name} and $parentList) {
                                 $assetPermission->{$setterName}(true);
                             } else {
                                 if (isset($node->{$name})) {
                                     $assetPermission->{$setterName}(false);
                                     //if no list permission set all to false
                                     if ($name == "list") {
                                         foreach ($permissionNames as $n) {
                                             $setterName = "set" . ucfirst($n);
                                             $assetPermission->{$setterName}(false);
                                         }
                                         break;
                                     }
                                 }
                             }
                         }
                         $assetPermission->save();
                         if ($node->evictChildrenPermissions) {
                             $successorList = new Asset_List();
                             $successorList->setOrderKey("filename");
                             $successorList->setOrder("asc");
                             if ($asset->getParentId() < 1) {
                                 $successorList->setCondition("parentId > 0");
                             } else {
                                 $successorList->setCondition("path like '" . $asset->getFullPath() . "/%'");
                             }
                             Logger::debug($successorList->getCondition());
                             $successors = $successorList->load();
                             foreach ($successors as $successor) {
                                 $permission = $successor->getPermissionsForUser($user);
                                 if ($permission->getId() > 0 and $permission->getCid() == $successor->getId()) {
                                     $permission->delete();
                                 }
                             }
                         }
                     }
                 }
             }
             $this->_helper->json(array("success" => true));
         }
     } else {
         if ($this->_getParam("xaction") == "destroy") {
             //ignore
         } else {
             //read
             if ($user instanceof User) {
                 $userPermissionsNamespace = new Zend_Session_Namespace('assetUserPermissions');
                 if (!isset($userPermissionsNamespace->expandedNodes) or $userPermissionsNamespace->currentUser != $user->getId()) {
                     $userPermissionsNamespace->currentUser = $user->getId();
                     $userPermissionsNamespace->expandedNodes = array();
                 }
                 if (is_numeric($this->_getParam("anode")) and $this->_getParam("anode") > 0) {
                     $node = $this->_getParam("anode");
                     $asset = Asset::getById($node);
                     if ($user instanceof User and $asset->hasChilds()) {
                         $list = new Asset_List();
                         $list->setCondition("parentId = ?", $asset->getId());
                         $list->setOrderKey("filename");
                         $list->setOrder("asc");
                         $childsList = $list->load();
                         $requestedNodes = array();
                         foreach ($childsList as $child) {
                             $requestedNodes[] = $child->getId();
                         }
                         $userPermissionsNamespace->expandedNodes = array_merge($userPermissionsNamespace->expandedNodes, $requestedNodes);
                     }
                 } else {
                     $userPermissionsNamespace->expandedNodes = array_merge($userPermissionsNamespace->expandedNodes, array(1));
                 }
                 //load all nodes which are open in client
                 $assetList = new Asset_List();
                 $assetList->setOrderKey("filename");
                 $assetList->setOrder("asc");
                 $queryIds = "'" . implode("','", $userPermissionsNamespace->expandedNodes) . "'";
                 $assetList->setCondition("id in (" . $queryIds . ")");
                 $o = $assetList->load();
                 $total = count($o);
                 $assets = array();
                 foreach ($o as $asset) {
                     if ($asset->getParentId() > 0) {
                         $parent = Asset::getById($asset->getParentId());
                     } else {
                         $parent = null;
                     }
                     // get current user permissions
                     $asset->getPermissionsForUser($this->getUser());
                     // only display asset if listing is allowed for the current user
                     if ($asset->isAllowed("list") and $asset->isAllowed("permissions")) {
                         $treeNodePermissionConfig = $this->getTreeNodePermissionConfig($user, $asset, $parent, true);
                         $assets[] = $treeNodePermissionConfig;
                         $tmpAssets[$asset->getId()] = $treeNodePermissionConfig;
                     }
                 }
                 //only visible nodes and in the order how they should be displayed ... doesn't make sense but seems to fix bug of duplicate nodes
                 $assetsForFrontend = array();
                 $visible = $this->_getParam("visible");
                 if ($visible) {
                     $visibleNodes = explode(",", $visible);
                     foreach ($visibleNodes as $nodeId) {
                         $assetsForFrontend[] = $tmpAssets[$nodeId];
                         if ($nodeId == $this->_getParam("anode") and is_array($requestedNodes)) {
                             foreach ($requestedNodes as $nId) {
                                 $assetsForFrontend[] = $tmpAssets[$nId];
                             }
                         }
                     }
                     $assets = $assetsForFrontend;
                 }
             }
             $this->_helper->json(array("total" => $total, "data" => $assets, "success" => true));
         }
     }
 }