Pluf Framework

Pluf Framework Git Source Tree

Root/src/Pluf/Precondition.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 Plume Framework, a simple PHP Application Framework.
6# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
7#
8# Plume Framework is free software; you can redistribute it and/or modify
9# it under the terms of the GNU Lesser General Public License as published by
10# the Free Software Foundation; either version 2.1 of the License, or
11# (at your option) any later version.
12#
13# Plume Framework 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 Lesser General Public License for more details.
17#
18# You should have received a copy of the GNU Lesser 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 * Ready to use preconditions to test if the user is logged in, etc.
26 */
27class Pluf_Precondition
28{
29 /**
30 * Check if the user is logged in.
31 *
32 * Returns a redirection to the login page, but if not active
33 * returns a forbidden error.
34 *
35 * @param Pluf_HTTP_Request
36 * @return mixed
37 */
38 static public function loginRequired($request)
39 {
40 if (!isset($request->user) or $request->user->isAnonymous()) {
41 return new Pluf_HTTP_Response_RedirectToLogin($request);
42 }
43 if (!$request->user->active) {
44 return new Pluf_HTTP_Response_Forbidden($request);
45 }
46 return true;
47 }
48
49 /**
50 * Check if the user is admin or staff.
51 *
52 * @param Pluf_HTTP_Request
53 * @return mixed
54 */
55 static public function staffRequired($request)
56 {
57 $res = Pluf_Precondition::loginRequired($request);
58 if (true !== $res) {
59 return $res;
60 }
61 if ($request->user->administrator or $request->user->staff) {
62 return true;
63 }
64 return new Pluf_HTTP_Response_Forbidden($request);
65 }
66
67 /**
68 * Check if the user is administrator..
69 *
70 * @param Pluf_HTTP_Request
71 * @return mixed
72 */
73 static public function adminRequired($request)
74 {
75 $res = Pluf_Precondition::loginRequired($request);
76 if (true !== $res) {
77 return $res;
78 }
79 if ($request->user->administrator) {
80 return true;
81 }
82 return new Pluf_HTTP_Response_Forbidden($request);
83 }
84
85 /**
86 * Check if the user has a given permission..
87 *
88 * @param Pluf_HTTP_Request
89 * @param string Permission
90 * @return mixed
91 */
92 static public function hasPerm($request, $permission)
93 {
94 $res = Pluf_Precondition::loginRequired($request);
95 if (true !== $res) {
96 return $res;
97 }
98 if ($request->user->hasPerm($permission)) {
99 return true;
100 }
101 return new Pluf_HTTP_Response_Forbidden($request);
102 }
103
104 /**
105 * Requires SSL to access the view.
106 *
107 * It will redirect the user to the same URL but over SSL if the
108 * user is not using SSL, if POST request, the data are lost, so
109 * handle it with care.
110 *
111 * @param Pluf_HTTP_Request
112 * @return mixed
113 */
114 static public function sslRequired($request)
115 {
116 if (empty($_SERVER['HTTPS']) or $_SERVER['HTTPS'] == 'off') {
117 return new Pluf_HTTP_Response_Redirect('https://'.$request->http_host.$request->uri);
118 }
119 return true;
120 }
121
122}

Archive Download this file

Branches

Tags