InDefero

Sign in or create your account | Project List | Help

InDefero Git Source Tree

Root/src/IDF/Middleware.php

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
24/**
25 * Project middleware.
26 *
27 * It must be after the session middleware.
28 */
29class IDF_Middleware
30{
31    /**
32     * Process the request.
33     *
34     * When processing the request, check if matching a project. If
35     * so, directly set $request->project to the project.
36     *
37     * The url to match a project is in the format /p/(\w+)/whatever
38     * or /api/p/(\w+)/whatever. This means that it will not try to
39     * match on /login/ or /logout/.
40     *
41     * @param Pluf_HTTP_Request The request
42     * @return bool false or redirect.
43     */
44    function process_request(&$request)
45    {
46        $match = array();
47        if (preg_match('#^/(?:api/p|p)/([\-\w]+)/?#', $request->query, $match)) {
48            try {
49                $request->project = IDF_Project::getOr404($match[1]);
50            } catch (Pluf_HTTP_Error404 $e) {
51                return new Pluf_HTTP_Response_NotFound($request);
52            }
53            $request->conf = new IDF_Conf();
54            $request->conf->setProject($request->project);
55            self::setRights($request);
56        }
57        return false;
58    }
59
60    public static function setRights(&$request)
61    {
62        $ak = array('downloads_access_rights' => 'hasDownloadsAccess',
63                    'wiki_access_rights' => 'hasWikiAccess',
64                    'review_access_rights' => 'hasReviewAccess',
65                    'source_access_rights' => 'hasSourceAccess',
66                    'issues_access_rights' => 'hasIssuesAccess');
67        $request->rights = array();
68        foreach ($ak as $key=>$val) {
69            $request->rights[$val] = (true === IDF_Precondition::accessTabGeneric($request, $key));
70        }
71    }
72
73    /**
74     * Update the template tags and modifiers to not have them in the config.
75     *
76     * This is here at the moment because we do not want to put that
77     * in a IDF_Template class just for one method.
78     *
79     */
80    public static function updateTemplateTagsModifiers($sender, &$params)
81    {
82        $params['tags'] = array_merge($params['tags'],
83                                      array(
84                              'hotkey' => 'IDF_Template_HotKey',
85                              'issuetext' => 'IDF_Template_IssueComment',
86                              'timeline' => 'IDF_Template_TimelineFragment',
87                              'markdown' => 'IDF_Template_Markdown',
88                              'showuser' => 'IDF_Template_ShowUser',
89                              'ashowuser' => 'IDF_Template_AssignShowUser',
90                                            ));
91        $params['modifiers'] = array_merge($params['modifiers'],
92                                           array(
93                                   'size' => 'IDF_Views_Source_PrettySize',
94                                   'ssize' => 'IDF_Views_Source_PrettySizeSimple',
95                                   'shorten' => 'IDF_Views_Source_ShortenString',
96                                                 ));
97    }
98}
99
100
101function IDF_Middleware_ContextPreProcessor($request)
102{
103    $c = array();
104    $c['request'] = $request;
105    $c['isAdmin'] = ($request->user->administrator or $request->user->staff);
106    if (isset($request->project)) {
107        $c['project'] = $request->project;
108        $c['isOwner'] = $request->user->hasPerm('IDF.project-owner',
109                                                $request->project);
110        $c['isMember'] = $request->user->hasPerm('IDF.project-member',
111                                                 $request->project);
112        $c = array_merge($c, $request->rights);
113    }
114    $c['usherConfigured'] = Pluf::f("mtn_usher_conf", null) !== null;
115    return $c;
116}
117
118

Archive Download this file

Branches:
dev
develop
master
newdiff
svn

Tags:
v1.0