function testGetTargetsSpecific()
 {
     $rat = Role_Action_Target::getOneFromDb(['role_action_target_link_id' => 212], $this->DB);
     $targets = $rat->getTargets();
     $this->assertEqual(1, count($targets));
     $this->assertEqual(1004, $targets[0]->notebook_id);
 }
 public function getRoleActionTargets()
 {
     $rats = Role_Action_Target::getAllFromDb(['role_id' => $this->role_id], $this->dbConnection);
     usort($rats, 'Role_Action_Target::cmp');
     return $rats;
 }
 function testCanActOnTarget_Pub_Verify()
 {
     $n2 = Notebook::getOneFromDb(['notebook_id' => 1003], $this->DB);
     // owned by 102
     $actions_list = Action::getAllFromDb([], $this->DB);
     $actions = [];
     foreach ($actions_list as $act_elt) {
         $actions[$act_elt->name] = $act_elt;
     }
     $rat = new Role_Action_Target(['last_user_id' => 110, 'role_id' => 3, 'action_id' => 1, 'target_type' => 'notebook', 'target_id' => 1003, 'DB' => $this->DB]);
     $rat->updateDb();
     $this->assertTrue($rat->matchesDb);
     // basic, field user
     $u = User::getOneFromDb(['user_id' => 101], $this->DB);
     $this->assertFalse($n2->flag_workflow_published);
     $this->assertFalse($n2->flag_workflow_validated);
     $this->assertFalse($u->canActOnTarget($actions['view'], $n2));
     $this->assertFalse($u->canActOnTarget($actions['edit'], $n2));
     $this->assertTrue($u->canActOnTarget($actions['create'], $n2));
     $this->assertFalse($u->canActOnTarget($actions['delete'], $n2));
     $this->assertFalse($u->canActOnTarget($actions['publish'], $n2));
     $this->assertFalse($u->canActOnTarget($actions['verify'], $n2));
     $n2->flag_workflow_published = true;
     $n2->updateDb();
     $this->assertTrue($n2->matchesDb);
     $u->clearCaches();
     $this->assertFalse($u->canActOnTarget($actions['view'], $n2));
     $this->assertFalse($u->canActOnTarget($actions['edit'], $n2));
     $this->assertTrue($u->canActOnTarget($actions['create'], $n2));
     $this->assertFalse($u->canActOnTarget($actions['delete'], $n2));
     $this->assertFalse($u->canActOnTarget($actions['publish'], $n2));
     $this->assertFalse($u->canActOnTarget($actions['verify'], $n2));
     $n2->flag_workflow_validated = true;
     $n2->updateDb();
     $this->assertTrue($n2->matchesDb);
     $u->clearCaches();
     $this->assertTrue($u->canActOnTarget($actions['view'], $n2));
     $this->assertFalse($u->canActOnTarget($actions['edit'], $n2));
     $this->assertTrue($u->canActOnTarget($actions['create'], $n2));
     $this->assertFalse($u->canActOnTarget($actions['delete'], $n2));
     $this->assertFalse($u->canActOnTarget($actions['publish'], $n2));
     $this->assertFalse($u->canActOnTarget($actions['verify'], $n2));
 }
 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;
 }
 function testRenderAsListItem_Editable()
 {
     $ap = Authoritative_Plant::getOneFromDb(['authoritative_plant_id' => 5001], $this->DB);
     global $USER;
     $USER = User::getOneFromDb(['username' => TESTINGUSER], $this->DB);
     //$this->todo('make user able to edit the authoritative plant');
     $rat = new Role_Action_Target(['role_action_target_link_id' => 500, 'last_user_id' => 0, 'role_id' => 3, 'action_id' => 2, 'target_type' => 'global_plant', 'target_id' => 0, 'DB' => $this->DB]);
     $rat->updateDb();
     $this->assertTrue($rat->matchesDb);
     $canonical = '<li data-authoritative_plant_id="5001" data-created_at="' . $ap->created_at . '" data-updated_at="' . $ap->updated_at . '" ' . 'data-class="AP_A_class" data-order="AP_A_order" data-family="AP_A_family" data-genus="AP_A_genus" data-species="AP_A_species" data-variety="AP_A_variety" data-catalog_identifier="AP_1_CI" data-flag_active="1" data-flag_delete="0" data-can-edit="1"><i class="icon-ok"></i> <a href="/digitalfieldnotebooks/app_code/authoritative_plant.php?action=view&authoritative_plant_id=5001">' . htmlentities($ap->renderAsShortText()) . '</a></li>';
     $rendered = $ap->renderAsListItem();
     //            echo "<pre>\n".htmlentities($canonical)."\n".htmlentities($rendered)."\n</pre>";
     $this->assertEqual($canonical, $rendered);
     $rat->doDelete();
     unset($USER);
 }