<commit_before><?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 <NAME> and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */

/**
 * Ready to use preconditions to test if the user is logged in, etc.
 */
class Pluf_Precondition
{
    /**
     * Check if the user is logged in.
     *
     * Returns a redirection to the login page, but if not active
     * returns a forbidden error.
     *
     * @param Pluf_HTTP_Request
     * @return mixed
     */
    static public function loginRequired($request)
    {
        if (!isset($request->user) or $request->user->isAnonymous()) {
            return new Pluf_HTTP_Response_RedirectToLogin($request);
        }
        if (!$request->user->active) {
            return new Pluf_HTTP_Response_Forbidden($request);
        }
        return true;
    }

    /**
     * Check if the user is admin or staff.
     *
     * @param Pluf_HTTP_Request
     * @return mixed
     */
    static public function staffRequired($request)
    {
        $res = Pluf_Precondition::loginRequired($request);
        if (true !== $res) {
            return $res;
        }
        if ($request->user->administrator or $request->user->staff) {
            return true;
        }
        return new Pluf_HTTP_Response_Forbidden($request);
    }

    /**
     * Check if the user is administrator..
     *
     * @param Pluf_HTTP_Request
     * @return mixed
     */
    static public function adminRequired($request)
    {
        $res = Pluf_Precondition::loginRequired($request);
        if (true !== $res) {
            return $res;
        }
        if ($request->user->administrator) {
            return true;
        }
        return new Pluf_HTTP_Response_Forbidden($request);
    }

    /**
     * Check if the user has a given permission..
     *
     * @param Pluf_HTTP_Request
     * @param string Permission
     * @return mixed
     */
    static public function hasPerm($request, $permission)
    {
        $res = Pluf_Precondition::loginRequired($request);
        if (true !== $res) {
            return $res;
        }
        if ($request->user->hasPerm($permission)) {
            return true;
        }
        return new Pluf_HTTP_Response_Forbidden($request);
    }

}<commit_msg>Added a new SSL precondition.<commit_after><?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 <NAME> and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */

/**
 * Ready to use preconditions to test if the user is logged in, etc.
 */
class Pluf_Precondition
{
    /**
     * Check if the user is logged in.
     *
     * Returns a redirection to the login page, but if not active
     * returns a forbidden error.
     *
     * @param Pluf_HTTP_Request
     * @return mixed
     */
    static public function loginRequired($request)
    {
        if (!isset($request->user) or $request->user->isAnonymous()) {
            return new Pluf_HTTP_Response_RedirectToLogin($request);
        }
        if (!$request->user->active) {
            return new Pluf_HTTP_Response_Forbidden($request);
        }
        return true;
    }

    /**
     * Check if the user is admin or staff.
     *
     * @param Pluf_HTTP_Request
     * @return mixed
     */
    static public function staffRequired($request)
    {
        $res = Pluf_Precondition::loginRequired($request);
        if (true !== $res) {
            return $res;
        }
        if ($request->user->administrator or $request->user->staff) {
            return true;
        }
        return new Pluf_HTTP_Response_Forbidden($request);
    }

    /**
     * Check if the user is administrator..
     *
     * @param Pluf_HTTP_Request
     * @return mixed
     */
    static public function adminRequired($request)
    {
        $res = Pluf_Precondition::loginRequired($request);
        if (true !== $res) {
            return $res;
        }
        if ($request->user->administrator) {
            return true;
        }
        return new Pluf_HTTP_Response_Forbidden($request);
    }

    /**
     * Check if the user has a given permission..
     *
     * @param Pluf_HTTP_Request
     * @param string Permission
     * @return mixed
     */
    static public function hasPerm($request, $permission)
    {
        $res = Pluf_Precondition::loginRequired($request);
        if (true !== $res) {
            return $res;
        }
        if ($request->user->hasPerm($permission)) {
            return true;
        }
        return new Pluf_HTTP_Response_Forbidden($request);
    }

    /**
     * Requires SSL to access the view.
     *
     * It will redirect the user to the same URL but over SSL if the
     * user is not using SSL, if POST request, the data are lost, so
     * handle it with care.
     *
     * @param Pluf_HTTP_Request
     * @return mixed
     */
    static public function sslRequired($request)
    {
        if (empty($_SERVER['HTTPS']) or $_SERVER['HTTPS'] == 'off') {
            return new Pluf_HTTP_Response_Redirect('https://'.$request->http_host.$request->uri);
        }
        return true;
    }

}
{'fraction_non_alphanumeric': 0.10493102436160845, 'fraction_numerical': 0.0067508071617258586, 'mean_word_length': 2.3704253214638973, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3594567', 'n_tokens_mistral': 1930, 'n_tokens_neox': 1867, 'n_words': 659}