function testGetAccessibleNotebooksManager() { $u = User::getOneFromDb(['user_id' => 110], $this->DB); $r = $u->getRoles(); $this->assertTrue(in_array('manager', array_map(function ($e) { return $e->name; }, $r))); $actions = Action::getAllFromDb([], $this->DB); $this->assertEqual(8, count($actions)); $all_n = Notebook::getAllFromDb([], $this->DB); $num_all_n = count($all_n); foreach ($actions as $a) { //!!!!!!!!!!!!!!!!!!!!!!!!!!! // THIS IS THE MAIN TESTED ACTION $accessible_n = $u->getAccessibleNotebooks($a); //!!!!!!!!!!!!!!!!!!!!!!!!!!! $num_accessible_n = count($accessible_n); $this->assertEqual($num_all_n, $num_accessible_n, 'mismatch on notebooks accesible for action ' . $a->name . ": expecting {$num_all_n}, but got {$num_accessible_n} instead"); } }
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; }
public function getTargets() { switch ($this->target_type) { case 'global_notebook': return Notebook::getAllFromDb([], $this->dbConnection); break; case 'global_metadata': return Metadata_Structure::getAllFromDb([], $this->dbConnection); break; case 'global_plant': return Authoritative_Plant::getAllFromDb([], $this->dbConnection); break; case 'global_specimen': return Specimen::getAllFromDb([], $this->dbConnection); break; case 'notebook': return array(Notebook::getOneFromDb(['notebook_id' => $this->target_id], $this->dbConnection)); break; case 'metadata_structure': return array(Metadata_Structure::getOneFromDb(['metadata_structure_id' => $this->target_id], $this->dbConnection)); break; case 'plant': return array(Authoritative_Plant::getOneFromDb(['authoritative_id' => $this->target_id], $this->dbConnection)); break; case 'specimen': return array(Specimen::getOneFromDb(['specimen_id' => $this->target_id], $this->dbConnection)); break; default: return array(); } }