<?php

include_once './pages/PageInterface.inc.php';
include_once './pages/ErrorPage.inc.php';

include_once './includes/users/Member.inc.php';
include_once './includes/users/MemberLister.inc.php';

include_once './includes/users/userRights/MemberRightArray.inc.php';

include_once './includes/exceptions/AccessDeniedException.inc.php';

/**
 * This class acts as the gateway into all administrative pages.
 *
 * It has to check if the user has the sufficient rights to enter the pages.
 *
 * @author Thomas Schwery <thomas.schwery@epfl.ch>
 */
abstract class AdministrationPage implements PageInterface {

    public function  __construct() {        
        if ($this->checkUserRights(get_class($this))) {
            $this->constructAdmin();
        } else {
            throw new AccessDeniedException();
        }
    }

    abstract protected function constructAdmin();

    protected function checkUserRights($pageType) {
        if (key_exists("loggedin", $_SESSION) && ($_SESSION['loggedin'] == true)) {
            $username = $_SESSION['username'];
            $user = MemberLister::getByLogin($username);
            $adminRight = str_replace("Page", "", $pageType);

            return MemberRightArray::hasRightNamed($user, $adminRight);
        }
    }

    public static function getStaticTitle() {
        return "Administration";
    }
}
?>
