InDefero

Sign in or create your account | Project List | Help

InDefero Git Source Tree

Root/src/IDF/Precondition.php

Source at commit 7383e18dff19e7e6a1e56d55cdfbfa109e91bfb3 created 1 year 7 months ago.
By Loic d'Anterroches, Fixed issue 4, with fine control over the tabs access.
1<?php
2/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3/*
4# ***** BEGIN LICENSE BLOCK *****
5# This file is part of InDefero, an open source project management application.
6# Copyright (C) 2008 Céondo Ltd and contributors.
7#
8# InDefero is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# InDefero is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21#
22# ***** END LICENSE BLOCK ***** */
23
24class IDF_Precondition
25{
26    /**
27     * Check if the user is project owner.
28     *
29     * @param Pluf_HTTP_Request
30     * @return mixed
31     */
32    static public function projectOwner($request)
33    {
34        $res = Pluf_Precondition::loginRequired($request);
35        if (true !== $res) {
36            return $res;
37        }
38        if ($request->user->hasPerm('IDF.project-owner', $request->project)) {
39            return true;
40        }
41        return new Pluf_HTTP_Response_Forbidden($request);
42    }
43
44    /**
45     * Check if the user is project owner or member.
46     *
47     * @param Pluf_HTTP_Request
48     * @return mixed
49     */
50    static public function projectMemberOrOwner($request)
51    {
52        $res = Pluf_Precondition::loginRequired($request);
53        if (true !== $res) {
54            return $res;
55        }
56        if ($request->user->hasPerm('IDF.project-owner', $request->project)
57            or
58            $request->user->hasPerm('IDF.project-member', $request->project)
59            ) {
60            return true;
61        }
62        return new Pluf_HTTP_Response_Forbidden($request);
63    }
64
65    /**
66     * Check if the user can access a given element.
67     *
68     * The rights are:
69     * - 'all' (default)
70     * - 'none'
71     * - 'login'
72     * - 'members'
73     * - 'owners'
74     *
75     * The order of the rights is such that a 'owner' is also a
76     * 'member' and of course a logged in person.
77     *
78     * @param Pluf_HTTP_Request
79     * @param string Control key
80     * @return mixed
81     */
82    static public function accessTabGeneric($request, $key)
83    {
84        switch ($request->conf->getVal($key, 'all')) {
85        case 'none':
86            return new Pluf_HTTP_Response_Forbidden($request);
87        case 'login':
88            return Pluf_Precondition::loginRequired($request);
89        case 'members':
90            return self::projectMemberOrOwner($request);
91        case 'owners':
92            return self::projectOwner($request);
93        case 'all':
94        default:
95            return true;
96        }
97    }
98
99    static public function accessSource($request)
100    {
101        return self::accessTabGeneric($request, 'source_access_rights');
102    }
103
104    static public function accessIssues($request)
105    {
106        return self::accessTabGeneric($request, 'issues_access_rights');
107    }
108
109    static public function accessDownloads($request)
110    {
111        return self::accessTabGeneric($request, 'downloads_access_rights');
112    }
113}

Archive Download this file

Branches:
dev
master
newdiff
svn