/** * Update an existing employee * PUT /api/employee/:netID * * Fields to be updated should be provided in the body of the request * * Returns a model of the updated employee */ public function put($params) { $this->requireAuthentication(); if (count($params['url']) < 3) { $this->error("No netID provided", 400); } $netID = $params['url'][2]; $employeeAcc = new \TMT\accessor\Employee(); $model = $employeeAcc->get($netID); $allow_self_update = array("phone", "email"); $require_permission = false; foreach ($params['request'] as $key => $value) { if (property_exists($model, $key)) { switch ($key) { case "area": if (!$this->isSuperuser()) { $this->error($message, 403); exit; } $model->area = $value; break; case "netID": // Never update a netID continue; break; case "position": // Positions should only be updated from the default area if ($model->area == $this->user['area']) { $model->{$key} = $value; } break; default: if (!in_array($key, $allow_self_update)) { $require_permission = true; } $model->{$key} = $value; } } } if ($require_permission) { $this->forcePermission("update", "1450ff35-82a7-45ed-adcf-ffa254ebafa2"); } $employeeAcc->save($model); $this->respond($model); }
/** * Render view * * @param $view string The name of the view * @param $data array The data to use in rendering in the view */ public function render($view, $data = array()) { // Retrieve data necessary for properly rendering header and footer, and // add that data to the template data $areaAcc = new \TMT\accessor\AreaAccessor(); $employeeAcc = new \TMT\accessor\Employee(); $linkAcc = new \TMT\accessor\Links(); // Determine if user is admin or superuser $admin = $this->isAdmin(); $su = $this->isSuperuser(); // Get user and area information $user = $employeeAcc->get($this->user['netId']); $areaArray = $areaAcc->getAll($this->user['netId']); $areas = array(); if (isset($this->user['area'])) { foreach ($areaArray as $area) { $areas[] = array('id' => $area->ID, 'name' => $area->longName); } // Retrieve link tree $links = $linkAcc->getTree($this->user['area']); $this->cleanLinkTree($links, $admin, $su); } // Check environment $environment = $this->getEnvironment(); // Get quicklinks $quicklinks = $this->getAccessor("Quicklinks")->getByUser($this->user['netId']); $notificationsUrl = getenv("NOTIFICATIONSURL"); // Add data necessary for the main header and footer to load properly $data['templateData'] = array("area" => isset($this->user['area']) ? $this->user['area'] : null, "areaName" => isset($this->user['area']) ? $areaAcc->get($this->user['area'])->longName : null, "areaGuid" => isset($this->user['areaGuid']) ? $this->user['areaGuid'] : null, "areas" => $areas, "authenticated" => $this->authenticated, "canSU" => $this->canBeSuperuser(), "environment" => $environment, "firstName" => $user->firstName, "isSU" => $su, "jwt" => $this->createJWT(), "lastName" => $user->lastName, "links" => isset($links) ? $links : null, "netId" => $this->user['netId'], "notificationsUrl" => $notificationsUrl, "quicklinks" => $quicklinks, "server" => $_SERVER['SERVER_NAME']); // load twig $twigLoader = new \Twig_Loader_Filesystem(self::VIEWS_PATH); $twig = new \Twig_Environment($twigLoader); // to avoid conflicts with angularjs use of {{ }} $lexer = new \Twig_Lexer($twig, array('tag_comment' => array('[#', '#]'), 'tag_block' => array('[%', '%]'), 'tag_variable' => array('[[', ']]'), 'interpolation' => array('#[', ']'))); $twig->setLexer($lexer); // render a view echo $twig->render($view . self::VIEW_FILE_TYPE, $data); }
/** * @covers ::getByArea */ public function testGetByArea() { $accessor = new \TMT\accessor\Employee(); $employee = $accessor->get('inserted'); $employee->area = 2; $accessor->save($employee); $employees = $accessor->getByArea(2); $this->assertEquals(3, count($employees)); $employees = $accessor->getByArea(4); $this->assertEquals(0, count($employees)); $employee = $accessor->get('inserted'); $employee->active = 0; $accessor->save($employee); $employees = $accessor->getByArea(2, true, 0); $this->assertEquals(2, count($employees)); $employee = $accessor->get('inserted'); $employee->active = -1; $accessor->save($employee); $employees = $accessor->getByArea(2, true, -1); $this->assertEquals(1, count($employees)); $this->assertEquals('inserted', $employees[0]->netID); $employees = $accessor->getByArea(array(1, 2)); $this->assertEquals(4, count($employees)); $employees = $accessor->getByArea(array(1, 2), true, 1); $this->assertEquals(2, count($employees)); }