From d7ed30e1ba11cafa444188f71d5d507c9f57fece Mon Sep 17 00:00:00 2001 From: Eike Foken Date: Thu, 8 Sep 2011 03:21:54 +0200 Subject: [PATCH] Implement public projects --- application/config/form_validation.php | 5 +++ application/controllers/projects.php | 38 +++++++++++++++++---- application/helpers/MY_form_helper.php | 47 ++++++++++++++++++++++++++ application/models/project.php | 38 ++++++++++++++------- application/views/header.php | 12 +++++-- application/views/projects/new.php | 6 ++++ 6 files changed, 124 insertions(+), 22 deletions(-) create mode 100644 application/helpers/MY_form_helper.php diff --git a/application/config/form_validation.php b/application/config/form_validation.php index 5d4ab94..38f1eed 100644 --- a/application/config/form_validation.php +++ b/application/config/form_validation.php @@ -232,6 +232,11 @@ $config['projects/create'] = array( 'label' => _('Default configuration'), 'rules' => 'file_allowed_type[calc]', ), + array( + 'field' => 'public', + 'label' => _('Make the project public'), + 'rules' => 'integer', + ), ); /** diff --git a/application/controllers/projects.php b/application/controllers/projects.php index 5413ec6..d54fdf7 100644 --- a/application/controllers/projects.php +++ b/application/controllers/projects.php @@ -41,9 +41,13 @@ class Projects extends CI_Controller { * Shows a list of all projects. */ public function index() { - $tpl['projects'] = $this->project->getAll(); + if ($this->access->isAdmin()) { + $data['projects'] = $this->project->getAll(); + } else { + $data['projects'] = $this->project->getAccessible($this->session->userdata('user_id')); + } - $this->load->view('projects/list', $tpl); + $this->load->view('projects/list', $data); } /** @@ -55,6 +59,7 @@ class Projects extends CI_Controller { $data = array( 'name' => $this->input->post('name'), 'description' => $this->input->post('description'), + 'public' => $this->input->post('public'), ); $data['project_id'] = $this->project->create($data); @@ -112,15 +117,18 @@ class Projects extends CI_Controller { * @param integer $id The ID of the project to show */ public function detail($id) { - $this->load->helper('typography'); - $this->load->model('job'); - $project = $this->project->getById($id); if (!$project) { - $this->messages->add(_('The project could not be loaded.'), 'error'); - redirect('projects', 303); + show_404(); } + if (!$this->_checkAccess($id)) { // check if the user has access + show_error(_("Sorry, you don't have access to this project."), 403); + } + + $this->load->helper('typography'); + $this->load->model('job'); + $data['project'] = $project; $data['experiments'] = $this->experiment->getByProjectId($id); $data['jobs'] = $this->job->getRecent($id); @@ -134,10 +142,26 @@ class Projects extends CI_Controller { * @param integer $projectId */ public function delete($id) { + if (!$this->_checkAccess($id)) { // check if the user has access + show_error(_("Sorry, you don't have access to this project."), 403); + } + if ($this->project->delete($id)) { $this->messages->add(_('The project was deleted.'), 'success'); } redirect('projects', 303); } + /** + * Checks if users have access to a project. + * + * @param string $projectId + */ + private function _checkAccess($projectId) { + $project = $this->project->getById($projectId); + return $this->access->isAdmin() || $project['public'] == 1; + } } + +/* End of file projects.php */ +/* Location: ./application/controllers/projects.php */ diff --git a/application/helpers/MY_form_helper.php b/application/helpers/MY_form_helper.php new file mode 100644 index 0000000..a07b0bd --- /dev/null +++ b/application/helpers/MY_form_helper.php @@ -0,0 +1,47 @@ + + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * Extends CI's form helpers. + * + * @package ScattPort + * @subpackage Helpers + * @author Eike Foken + */ + +if (!function_exists('form_yesno')) { + /** + * Shows a yes-no selection. + * + * @param string $name + * @param mixed $selected + * @param string $extra + * @return string + */ + function form_yesno($name = '', $selected = array(), $extra = '') { + return form_dropdown($name, array('0' => _('No'), '1' => _('Yes')), $selected, $extra); + } +} + +/* End of file MY_form_helper.php */ +/* Location: ./application/helpers/MY_form_helper.php */ diff --git a/application/models/project.php b/application/models/project.php index 97f1fef..ece859e 100644 --- a/application/models/project.php +++ b/application/models/project.php @@ -79,30 +79,42 @@ class Project extends CI_Model { } /** - * Get all publicly available projects. + * Gets all publicly available projects. * - * @return array All public projects. + * @return array All public projects */ - private function getPublic() { - $query = $this->db->where(array('public' => '1')) - ->order_by('name', 'asc') - ->get('projects'); - + public function getPublic() { + $query = $this->db->order_by('name ASC')->get_where('projects', array('public' => 1)); return $this->_addShortNames($query->result_array()); } + /** + * Gets all accessible projects for a user. + * + * @param string $userId + * @return array All accessible projects + */ + public function getAccessible($userId) { + $this->db->where('public', 1); + $this->db->or_where('owner', $userId); + + return $this->getAll(); + } + /** * Get a specific project from the database. * * @param type $project_id The project to get. */ - public function getById($project_id) { - $result = $this->db->get_where('projects', array('id' => $project_id))->row_array(); - $this->db->where('id', $project_id)->update('projects', array( - 'lastaccess' => mysql_now(), - )); + public function getById($projectId) { + $result = $this->db->get_where('projects', array('id' => $projectId))->row_array(); + $this->db->where('id', $projectId)->update('projects', array('lastaccess' => mysql_now())); - return $this->_addShortName($result); + if ($result) { + return $this->_addShortName($result); + } else { + return false; + } } /** diff --git a/application/views/header.php b/application/views/header.php index a3bf8a5..8b50d23 100644 --- a/application/views/header.php +++ b/application/views/header.php @@ -110,9 +110,9 @@ diff --git a/application/views/projects/new.php b/application/views/projects/new.php index 8a3c317..d275301 100644 --- a/application/views/projects/new.php +++ b/application/views/projects/new.php @@ -44,6 +44,12 @@ +
  • + +
    + +
    +