<commit_before><?php
/*
 * Copyright 2010 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

require_once('Subject.php');
require_once('SecurityManager.php');


/**
 * 
 * @author <NAME>
 */ 
class DefaultSubject implements Subject {

    /**
     * Session key to use.
     */
    const PRINCIPAL_KEY = "__DEFAULT_SUBJECT_PRINCIPAL";

    /**
     * @var SecurityManager
     */
    private $securityManager;

    /**
     * @var array
     */
    private $roles = null;

    /**
     * @var array
     */
    private $permissions = null;

    function __construct(SecurityManager $securityManager) {
        $this->securityManager = $securityManager;
    }

    function loadRoles() {
        if ($this->roles == null) {
            $this->roles = $this->securityManager->getRoles($this);
        }
    }

    function loadPermissions() {
        if ($this->permissions == null) {
            $this->permissions = $this->securityManager->getPermissions($this);
        }
    }

    public function checkPermission($permission) {
        if (!$this->hasPermission($permission)) {
            throw new NotAuthorizedException("Permission denied: $permission");
        }
    }

    public function checkRole($role) {
        if (!$this->hasRole($role)) {
            throw new NotAuthorizedException("Role denied: $role");
        }
    }

    public function getPrincipal() {
        return $_SESSION[self::PRINCIPAL_KEY];
    }

    public function hasPermission($permission) {
        $this->loadPermissions();
        return in_array($permission, $this->permissions);
    }

    public function hasRole($role) {
        $this->loadRoles();
        return in_array($role, $this->roles);
    }

    public function isAuthenticated() {
        return $this->securityManager->isAuthenticated($this);
    }

    public function login($credentials) {
        $_SESSION[self::PRINCIPAL_KEY] = $this->securityManager->login($this, $credentials);
        $this->roles = null;
        $this->permissions = null;
    }

    public function logout($principal = null) {
        $_SESSION[self::PRINCIPAL_KEY] = $this->securityManager->logout($this, $principal);
        $this->roles = null;
        $this->permissions = null;
    }
}<commit_msg>use new session interface for storing session values<commit_after><?php
/*
 * Copyright 2010 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
require_once('typesafe/session/Session.php');
require_once('Subject.php');
require_once('SecurityManager.php');


/**
 * 
 * @author <NAME>
 */ 
class DefaultSubject implements Subject {

    /**
     * Session key to use.
     */
    const PRINCIPAL_KEY = "__DEFAULT_SUBJECT_PRINCIPAL";

    /**
     * @var SecurityManager
     */
    private $securityManager;

    /**
     * @var Session
     */
    private $session;

    /**
     * @var array
     */
    private $roles = null;

    /**
     * @var array
     */
    private $permissions = null;

    function __construct(SecurityManager $securityManager, Session $session) {
        $this->securityManager = $securityManager;
        $this->session = $session;
    }

    function loadRoles() {
        if ($this->roles == null) {
            $this->roles = $this->securityManager->getRoles($this);
        }
    }

    function loadPermissions() {
        if ($this->permissions == null) {
            $this->permissions = $this->securityManager->getPermissions($this);
        }
    }

    public function checkPermission($permission) {
        if (!$this->hasPermission($permission)) {
            throw new NotAuthorizedException("Permission denied: $permission");
        }
    }

    public function checkRole($role) {
        if (!$this->hasRole($role)) {
            throw new NotAuthorizedException("Role denied: $role");
        }
    }

    public function getPrincipal() {
        return $this->session->get(self::PRINCIPAL_KEY);
    }

    public function hasPermission($permission) {
        $this->loadPermissions();
        return in_array($permission, $this->permissions);
    }

    public function hasRole($role) {
        $this->loadRoles();
        return in_array($role, $this->roles);
    }

    public function isAuthenticated() {
        return $this->securityManager->isAuthenticated($this);
    }

    public function login($credentials) {
        $principal = $this->securityManager->login($this, $credentials);
        $this->session->set(self::PRINCIPAL_KEY, $principal);
        $this->roles = null;
        $this->permissions = null;
    }

    public function logout($principal = null) {
        $principal = $this->securityManager->logout($this, $principal);
        $this->session->set(self::PRINCIPAL_KEY, $principal);
        $this->roles = null;
        $this->permissions = null;
    }
}
{'fraction_non_alphanumeric': 0.12741379310344828, 'fraction_numerical': 0.002758620689655172, 'mean_word_length': 2.7162075592568864, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5707095', 'n_tokens_mistral': 1646, 'n_tokens_neox': 1599, 'n_words': 410}