public function getRoleActionTargets() { $rats = Role_Action_Target::getAllFromDb(['role_id' => $this->role_id], $this->dbConnection); usort($rats, 'Role_Action_Target::cmp'); return $rats; }
function testCmp() { $rat1 = Role_Action_Target::getOneFromDb(['role_action_target_link_id' => 209], $this->DB); $rat2 = Role_Action_Target::getOneFromDb(['role_action_target_link_id' => 210], $this->DB); $this->assertEqual(Role_Action_Target::cmp($rat1, $rat2), -1); $this->assertEqual(Role_Action_Target::cmp($rat1, $rat1), 0); $this->assertEqual(Role_Action_Target::cmp($rat2, $rat1), 1); $all = Role_Action_Target::getAllFromDb(['role_action_target_link_id >' => '100'], $this->DB); usort($all, 'Role_Action_Target::cmp'); // // role, then action, then type, then target // SORT_PRIORITIES_FOR_TYPES = [ // 'global_notebook'=>10, // 'global_metadata'=>20, // 'global_plant' =>30, // 'global_specimen'=>40, // 'notebook' =>50, // 'metadata_structure' =>60, // 'plant' =>70, // 'specimen' =>80 // ]; //---------------------------------------- // (201,NOW(),NOW(), 110, 2, 1, 'global_notebook', 0, 0), // (206,NOW(),NOW(), 110, 2, 1, 'global_metadata', 0, 0), // (209,NOW(),NOW(), 110, 2, 1, 'global_plant', 0, 0), // (202,NOW(),NOW(), 110, 2, 2, 'global_notebook', 0, 0), // (203,NOW(),NOW(), 110, 2, 2, 'global_metadata', 0, 0), // (204,NOW(),NOW(), 110, 2, 2, 'global_plant', 0, 0), // (205,NOW(),NOW(), 110, 2, 2, 'global_specimen', 0, 0), // (215,NOW(),NOW(), 110, 2, 3, 'global_notebook', 0, 0), // (214,NOW(),NOW(), 110, 2, 3, 'global_metadata', 0, 0), // (216,NOW(),NOW(), 110, 2, 3, 'global_specimen', 0, 0), // (218,NOW(),NOW(), 110, 2, 4, 'global_notebook', 0, 0), // (217,NOW(),NOW(), 110, 2, 4, 'global_metadata', 0, 0), // (219,NOW(),NOW(), 110, 2, 4, 'global_specimen', 0, 0), // (207,NOW(),NOW(), 110, 3, 1, 'global_metadata', 0, 0), // (210,NOW(),NOW(), 110, 3, 1, 'global_plant', 0, 0), // (212,NOW(),NOW(), 110, 3, 1, 'notebook', 1004, 0), // (220,NOW(),NOW(), 110, 3, 4, 'global_notebook', 0, 0), // (221,NOW(),NOW(), 110, 3, 4, 'global_specimen', 0, 0) // (208,NOW(),NOW(), 110, 4, 1, 'global_metadata', 0, 0), // (211,NOW(),NOW(), 110, 4, 1, 'global_plant', 0, 0), // (213,NOW(),NOW(), 110, 4, 1, 'notebook', 1004, 0), $this->assertEqual(201, $all[0]->role_action_target_link_id); $this->assertEqual(206, $all[1]->role_action_target_link_id); $this->assertEqual(209, $all[2]->role_action_target_link_id); $this->assertEqual(202, $all[3]->role_action_target_link_id); $this->assertEqual(203, $all[4]->role_action_target_link_id); $this->assertEqual(204, $all[5]->role_action_target_link_id); $this->assertEqual(205, $all[6]->role_action_target_link_id); $this->assertEqual(215, $all[7]->role_action_target_link_id); $this->assertEqual(214, $all[8]->role_action_target_link_id); $this->assertEqual(216, $all[9]->role_action_target_link_id); $this->assertEqual(218, $all[10]->role_action_target_link_id); $this->assertEqual(217, $all[11]->role_action_target_link_id); $this->assertEqual(219, $all[12]->role_action_target_link_id); $this->assertEqual(207, $all[13]->role_action_target_link_id); $this->assertEqual(210, $all[14]->role_action_target_link_id); $this->assertEqual(212, $all[15]->role_action_target_link_id); $this->assertEqual(220, $all[16]->role_action_target_link_id); $this->assertEqual(221, $all[17]->role_action_target_link_id); $this->assertEqual(208, $all[18]->role_action_target_link_id); $this->assertEqual(211, $all[19]->role_action_target_link_id); $this->assertEqual(213, $all[20]->role_action_target_link_id); }
public function getAccessibleNotebooks($for_action, $debug_flag = 0) { if ($this->flag_is_system_admin) { if ($debug_flag) { echo "user is system admin<br/>\n"; } return Notebook::getAllFromDb(['flag_delete' => FALSE], $this->dbConnection); } if (is_string($for_action)) { global $ACTIONS; $for_action = $ACTIONS[$for_action]; } //util_prePrintR($this); $accessible_notebooks_ids = array(); if ($for_action->name == 'view' || $for_action->name == 'list') { $all_notebooks = Notebook::getAllFromDb(['flag_workflow_published' => TRUE, 'flag_workflow_validated' => TRUE, 'flag_delete' => FALSE], $this->dbConnection); $accessible_notebooks_ids = Db_Linked::arrayOfAttrValues($all_notebooks, 'notebook_id'); } $roles = $this->getRoles(); if ($debug_flag) { echo "user roles are<br/>\n"; util_prePrintR($roles); } foreach (Db_Linked::arrayOfAttrValues($roles, 'role_id') as $role_id) { $global_check = Role_Action_Target::getAllFromDb(['role_id' => $role_id, 'action_id' => $for_action->action_id, 'target_type' => 'global_notebook'], $this->dbConnection); if ($debug_flag) { echo "global_check is <br/>\n"; util_prePrintR($global_check); } if (count($global_check) > 0) { $all_notebooks = Notebook::getAllFromDb(['flag_delete' => FALSE], $this->dbConnection); $accessible_notebooks_ids = Db_Linked::arrayOfAttrValues($all_notebooks, 'notebook_id'); } $role_action_targets = Role_Action_Target::getAllFromDb(['role_id' => $role_id, 'action_id' => $for_action->action_id, 'target_type' => 'notebook'], $this->dbConnection); foreach ($role_action_targets as $rat) { if (!in_array($rat->target_id, $accessible_notebooks_ids)) { $accessible_notebooks_ids[] = $rat->target_id; } } } // util_prePrintR($accessible_notebooks_ids); $owned_notebooks = Notebook::getAllFromDb(['user_id' => $this->user_id], $this->dbConnection); $owned_notebook_ids = Db_Linked::arrayOfAttrValues($owned_notebooks, 'notebook_id'); $additional_notebook_ids = array(); foreach ($accessible_notebooks_ids as $an_id) { if (!in_array($an_id, $owned_notebook_ids)) { $additional_notebook_ids[] = $an_id; } } $additional_notebooks = array(); if (count($additional_notebook_ids) > 0) { $additional_notebooks = Notebook::getAllFromDb(['notebook_id' => $additional_notebook_ids], $this->dbConnection); } $ret = array_merge($owned_notebooks, $additional_notebooks); // util_prePrintR($accessible_notebooks_ids); return $ret; }