/** * Save project. */ public function saveAction() { $project = Project::find($this->currentProject['id']); $project->set($this->projectParams()); if ($project->save()) { return $this->redirectTo('project_settings'); } else { return $this->render('project_settings/options/index.phtml', ['proj' => $project]); } }
public function __construct() { // parent::__construct(); session_start(); $this->db = $GLOBALS['db']; $this->title(setting('title')); $this->set('traq', $this); // Is this an overlay request? if (Request::$headers->has('X-Overlay')) { $this->isOverlay = true; $this->layout = false; } // Are we on a project page? if ($projectSlug = Request::$properties->get('pslug')) { $this->currentProject = Project::where('slug = ?')->setParameter(0, $projectSlug)->fetch(); } // Is the user logged in? if (isset($_COOKIE['traq']) && ($sessionHash = $_COOKIE['traq'])) { $user = User::select('u.*', 'g.is_admin')->leftJoin('u', PREFIX . 'usergroups', 'g', 'g.id = u.group_id'); // Project role if ($this->currentProject) { $user->addSelect('r.project_role_id')->leftJoin('u', PREFIX . 'user_roles', 'r', 'r.user_id = u.id'); } // By session if ($sessionHash) { $user->where('u.login_hash = :login_hash')->setParameter('login_hash', $sessionHash); } // By API key // if ($apiKey) { // } $this->currentUser = $user->fetch(); } // Set current user $GLOBALS['currentUser'] = $this->currentUser; $this->set('currentUser', $this->currentUser); // Set current project $GLOBALS['currentProject'] = $this->currentProject; $this->set('currentProject', $this->currentProject); // Set title if ($this->currentProject) { $this->title($this->currentProject['name']); } // Check permission $this->before('*', function () use($projectSlug) { // Check if project exists if ($projectSlug && !$this->currentProject || $projectSlug && !$this->hasPermission('view')) { return $this->show404(); } }); $this->before('*', function () { if ($this->currentUser && $this->currentUser['password_ver'] == 'sha1' && Request::$properties->get('controller') != 'Traq\\Controllers\\UserCP' && Request::$properties->get('controller') != 'Traq\\Controllers\\Sessions') { return $this->redirectTo('usercp_password'); } }); }
/** * Always call this when defining `__construct()` in sub-classes. */ public function __construct() { $this->db = ConnectionManager::getConnection(); // Modal? if (Request::$headers->has('X-Modal')) { $this->isModal = Request::$headers->get('X-Modal') == true; } // Get current project. if (Request::$properties->has('pslug')) { $this->currentProject = Project::find('slug', Request::$properties->get('pslug')) ?: null; $GLOBALS['current_project'] = $this->currentProject; $this->before('*', function () { if (!$this->hasPermission('view', $this->currentProject)) { return $this->show404(); } }); } else { $GLOBALS['current_project'] = null; } // Get current user. if ($sessionHash = Request::$cookies->get('traq')) { if ($this->currentProject) { $user = User::select('u.*')->addSelect('pur.project_role_id')->leftJoin('u', UserRole::tableName(), 'pur', 'pur.project_id = :project_id AND pur.user_id = u.id'); $user->where('u.session_hash = :session_hash'); $user->setParameter('project_id', $this->currentProject['id']); $user->setParameter('session_hash', $sessionHash); $this->currentUser = $user->fetch() ?: null; } else { $this->currentUser = User::find('session_hash', $sessionHash) ?: null; } $GLOBALS['current_user'] = $this->currentUser; } else { $GLOBALS['current_user'] = null; } $GLOBALS['permissions'] = Permission::getPermissions($this->currentUser, $this->currentProject); // Add Traq as first breadcrumb. $this->addCrumb(setting('title'), $this->generateUrl('root')); // Check if the user has permission to view the current project if (isset($this->currentProject)) { $this->before('*', function () { if (!$this->hasPermission('view')) { return $this->show403(); } }); } // If the user has a `sha1` hashed password, require them to change it because // as of Traq 4.1, only mcrypt passwords will work. if ($this->currentUser['password_ver'] == 'sha1') { $this->before('*', function () { if (Request::$properties['controller'] != 'Traq\\Controllers\\UserCP' && Request::$properties['controller'] != 'Traq\\Controllers\\Sessions') { return $this->redirectTo('usercp_password'); } }); } }
/** * Project listing. */ public function indexAction() { $projects = []; foreach (Project::all() as $project) { if ($this->hasPermission('view', $project)) { $projects[] = $project; } } return $this->respondTo(function ($format) use($projects) { if ($format == 'html') { return $this->render('projects/index.phtml', ['projects' => $projects]); } elseif ($format == 'json') { return $this->jsonResponse($projects); } }); }
/** * Returns the subscribed object. * * @return object */ public function object() { if ($this->object !== null) { return $this->object; } switch ($this->type) { case 'project': $this->object = Project::find($this->object_id); break; case 'milestone': $this->object = Milestone::find($this->object_id); break; case 'ticket': $this->object = Ticket::find($this->object_id); break; } return $this->object; }
/** * Returns options for the specific ticket filter. * * @param string $filter * * @return array */ public static function selectOptionsFor($filter, Project $project) { switch ($filter) { // Milestone options case 'milestone': $options = $project->milestoneSelectOptions('slug'); break; // Version options // Version options case 'version': $options = $project->milestoneSelectOptions('slug'); break; // Type options // Type options case 'type': $options = Type::selectOptions('name'); break; // Status options // Status options case 'status': $options = Status::selectOptions('name'); break; // Component options // Component options case 'component': $options = Component::selectOptions($project->id, 'name'); break; // Priority options // Priority options case 'priority': $options = Priority::selectOptions('name'); break; // Severity options // Severity options case 'severity': $options = Severity::selectOptions('name'); break; } return $options; }
<?php use Traq\Models\Project; $testSuite->createGroup('Models / Project', function ($g) { $project = new Project(['name' => 'Project Model Test', 'slug' => 'project-model-test']); $g->test('Create', function ($t) use($project) { $t->assertTrue($project->save()); }); $g->test('Update', function ($t) use($project) { $project['name'] = 'Project Model Test - Updated'; $t->assertTrue($project->save()); }); $g->test('Slug in use', function ($t) { $project = new Project(['slug' => 'project-model-test']); $t->assertFalse($project->save()); $t->assertEquals('Slug is already in use', $project->getError('slug')[0]); }); $g->test('Get select options', function ($t) { $options = Project::selectOptions(); $t->assertArray($options); }); $g->test('Delete', function ($t) use($project) { $project->delete(); $t->assertFalse(Project::find('slug', 'project-model-test')); }); });
/*! * Traq Lite * Copyright (c) 2009-2016 Jack P. * https://github.com/nirix/traq-lite * * Licensed under the BSD 3-Clause license. */ use Traq\Models\Project; $query = db()->prepare('SELECT * FROM ' . PREFIX . 'projects WHERE id = ? LIMIT 1'); $query->bindValue(1, Request::$properties['id']); $query->execute(); $project = $query->fetch(); if (!$project) { return show404(); } $project = new Project($project); if (Request::$method == 'POST') { $project->set(['name' => Request::$post['name'], 'slug' => Request::$post['slug'], 'description' => Request::$post['description'], 'display_order' => Request::$post['display_order']]); if ($project->validate()) { db()->beginTransaction(); $query = db()->prepare(' UPDATE ' . PREFIX . 'projects SET name = :name, slug = :slug, description = :description, display_order = :display_order, updated_at = NOW() WHERE id = :id LIMIT 1 '); $query->bindValue(':id', $project['id']);
function createProject() { $project = new Project(['name' => 'project-' . mkRandomHash(5) . '-name', 'slug' => 'project-' . mkRandomHash(5) . '-slug']); $project->save(); return $project; }