<?php
/********************************************************************************
 * (c)1337 aheadware.com - All rights reserved
 ********************************************************************************/


class TB_Server_Core_AccessControl {

    // News - Related
    const RIGHT_NEWS_GET = 'NEWS-GET';
    const RIGHT_NEWS_GETUNREADCOUNT = 'NEWS-GETUNREADCOUNT';
    const RIGHT_NEWS_CREATECHAT = 'NEWS-CREATECHAT';
    const RIGHT_NEWS_DELETE = 'NEWS-DELETE';

    // Appointment - Related
    const RIGHT_APPOINTMENT_GETLIST = 'APPOINTMENT-GETLIST';
    const RIGHT_APPOINTMENT_CREATE = 'APPOINTMENT-CREATE';
    const RIGHT_APPOINTMENT_SUBSCRIBE = 'APPOINTMENT-SUBSCRIBE';
    const RIGHT_APPOINTMENT_CREATESERIAL = 'APPOINTMENT-CREATESERIAL';
    const RIGHT_APPOINTMENT_UPDATE = 'APPOINTMENT-UPDATE';
    const RIGHT_APPOINTMENT_DELETE = 'APPOINTMENT-DELETE';
    const RIGHT_APPOINTMENT_GETREMINDER = 'APPOINTMENT-GETREMINDER';
    const RIGHT_APPOINTMENT_SENDREMINDER = 'APPOINTMENT-SENDREMINDER';
    const RIGHT_APPOINTMENT_GETDETAIL = 'APPOINTMENT-GETDETAIL';
    const RIGHT_APPOINTMENT_SETATTENDANCESTATUS = 'APPOINTMENT-SETATTENDANCESTATUS';
    const RIGHT_APPOINTMENT_SETATTENDANCESTATUSBYTRAINER = 'APPOINTMENT-SETATTENDANCESTATUSBYTRAINER';

    // Profile
    const RIGHT_PROFILE_GET = 'PROFILE-GET';
    const RIGHT_PROFILE_GETALL = 'PROFILE-GETALL';
    const RIGHT_PROFILE_CHANGE = 'PROFILE-CHANGE';
    const RIGHT_PROFILE_UPDATE = 'PROFILE-UPDATE';
    const RIGHT_PROFILE_UPDATEPIC = 'PROFILE-UPDATEPIC';
    const RIGHT_PROFILE_UPDATEPICBYTRAINER = 'PROFILE-UPDATEPICBYTRAINER';
    const RIGHT_PROFILE_UPDATEBYTRAINER = 'PROFILE-UPDATEBYTRAINER';
    const RIGHT_PROFILE_ADDNEWTEAM = 'PROFILE-ADDNEWTEAM';
    const RIGHT_PROFILE_DELETE = 'PROFILE-DELETE';
    const RIGHT_PROFILE_DELETEBYTRAINER = 'PROFILE-DELETEBYTRAINER';
    const RIGHT_PROFILE_GETCACHEUPDATE = 'PROFILE-GETCACHEUPDATE';

    // Team
    const RIGHT_TEAM_LOAD = "TEAM-LOAD";
    const RIGHT_TEAM_GETCURRENT = "TEAM-GETCURRENT";
    const RIGHT_TEAM_UPDATE = "TEAM-UPDATE";
    const RIGHT_TEAM_UPDATEEMBLEM = "TEAM-UPDATEEMBLEM";
    const RIGHT_TEAM_INVITEPLAYER = "TEAM-INVITEPLAYER";
    const RIGHT_TEAM_DELETEINVITATION = "TEAM-DELETEINVITATION";
    const RIGHT_TEAM_RESENDINVITATION = "TEAM-RESENDINVITATION";
    const RIGHT_TEAM_CREATEPREMIUM = 'TEAM-CREATEPREMIUM';

    // Settings
    const RIGHT_SETTINGS_GETPREMIUMDETAILS = 'SETTINGS-GETPREMIUMDETAILS';
    const RIGHT_SETTINGS_GETPREMIUMDETAIL = 'SETTINGS-GETPREMIUMDETAIL';
    const RIGHT_SETTINGS_UPDATEPREMIUMDETAIL = 'SETTINGS-UPDATEPREMIUMDETAIL';
    const RIGHT_SETTINGS_GETBILLING = 'SETTINGS-GETBILLING';
    const RIGHT_SETTINGS_GETBILLINGDETAIL = 'SETTINGS-GETBILLINGDETAIL';
    const RIGHT_SETTINGS_CANCELSUBSCRIPTION = 'SETTINGS-CANCELSUBSCRIPTION';

    // Stats
    const RIGHT_STATS_DASHBOARD = 'STATS-DASHBOARD';

    // Account (Sidemenu)
    const RIGHT_ACCOUNT_REGISTERDEVICE = 'ACCOUNT-REGISTERDEVICE';
    const RIGHT_ACCOUNT_CHANGEPASSWORD = 'ACCOUNT-CHANGEPASSWORD';
    const RIGHT_ACCOUNT_GETEMAILCHANGE = 'ACCOUNT-GETEMAILCHANGE';
    const RIGHT_ACCOUNT_CHANGEEMAIL = 'ACCOUNT-CHANGEEMAIL';
    const RIGHT_ACCOUNT_CONFIRMEMAILCHANGE = 'ACCOUNT-CONFIRMEMAILCHANGE';

    // Content (Help, Support etc)
    const RIGHT_CONTENT_REQUESTSUPPORT = 'CONTENT-REQUESTSUPPORT';

    // Premium related rights
    protected static $PREMIUM = array(
        self::RIGHT_APPOINTMENT_CREATESERIAL,
        self::RIGHT_APPOINTMENT_SUBSCRIBE,
        self::RIGHT_PROFILE_UPDATEPIC,
        self::RIGHT_TEAM_UPDATEEMBLEM,
        self::RIGHT_APPOINTMENT_SETATTENDANCESTATUSBYTRAINER
    );

    // Coach
    protected static $TRAINER = array(
        self::RIGHT_NEWS_GET,
        self::RIGHT_NEWS_GETUNREADCOUNT,
        self::RIGHT_NEWS_CREATECHAT,
        self::RIGHT_NEWS_DELETE,

        self::RIGHT_APPOINTMENT_SUBSCRIBE,
        self::RIGHT_APPOINTMENT_CREATE,
        self::RIGHT_APPOINTMENT_CREATESERIAL,
        self::RIGHT_APPOINTMENT_DELETE,
        self::RIGHT_APPOINTMENT_GETDETAIL,
        self::RIGHT_APPOINTMENT_GETLIST,
        self::RIGHT_APPOINTMENT_GETREMINDER,
        self::RIGHT_APPOINTMENT_SENDREMINDER,
        self::RIGHT_APPOINTMENT_SETATTENDANCESTATUS,
        self::RIGHT_APPOINTMENT_SETATTENDANCESTATUSBYTRAINER,
        self::RIGHT_APPOINTMENT_UPDATE,

        self::RIGHT_PROFILE_ADDNEWTEAM,
        self::RIGHT_PROFILE_CHANGE,
        self::RIGHT_PROFILE_DELETE,
        self::RIGHT_PROFILE_DELETEBYTRAINER,
        self::RIGHT_PROFILE_GET,
        self::RIGHT_PROFILE_GETALL,
        self::RIGHT_PROFILE_UPDATE,
        self::RIGHT_PROFILE_UPDATEPIC,
        self::RIGHT_PROFILE_UPDATEPICBYTRAINER,
        self::RIGHT_PROFILE_UPDATEBYTRAINER,
        self::RIGHT_PROFILE_GETCACHEUPDATE,

        self::RIGHT_TEAM_DELETEINVITATION,
        self::RIGHT_TEAM_INVITEPLAYER,
        self::RIGHT_TEAM_LOAD,
        self::RIGHT_TEAM_RESENDINVITATION,
        self::RIGHT_TEAM_UPDATE,
        self::RIGHT_TEAM_UPDATEEMBLEM,
        self::RIGHT_TEAM_GETCURRENT,
        self::RIGHT_TEAM_CREATEPREMIUM,

        self::RIGHT_SETTINGS_GETBILLING,
        self::RIGHT_SETTINGS_GETBILLINGDETAIL,
        self::RIGHT_SETTINGS_GETPREMIUMDETAILS,
        self::RIGHT_SETTINGS_GETPREMIUMDETAIL,
        self::RIGHT_SETTINGS_UPDATEPREMIUMDETAIL,
        self::RIGHT_SETTINGS_CANCELSUBSCRIPTION,

        self::RIGHT_STATS_DASHBOARD,

        self::RIGHT_ACCOUNT_CHANGEEMAIL,
        self::RIGHT_ACCOUNT_CHANGEPASSWORD,
        self::RIGHT_ACCOUNT_CONFIRMEMAILCHANGE,
        self::RIGHT_ACCOUNT_GETEMAILCHANGE,
        self::RIGHT_ACCOUNT_REGISTERDEVICE,

        self::RIGHT_CONTENT_REQUESTSUPPORT
    );

    // Co-Coach
    protected static $COTRAINER = array(
        self::RIGHT_NEWS_GET,
        self::RIGHT_NEWS_GETUNREADCOUNT,
        self::RIGHT_NEWS_CREATECHAT,
        self::RIGHT_NEWS_DELETE,

        self::RIGHT_APPOINTMENT_SUBSCRIBE,
        self::RIGHT_APPOINTMENT_CREATE,
        self::RIGHT_APPOINTMENT_CREATESERIAL,
        self::RIGHT_APPOINTMENT_DELETE,
        self::RIGHT_APPOINTMENT_GETDETAIL,
        self::RIGHT_APPOINTMENT_GETLIST,
        self::RIGHT_APPOINTMENT_GETREMINDER,
        self::RIGHT_APPOINTMENT_SENDREMINDER,
        self::RIGHT_APPOINTMENT_SETATTENDANCESTATUS,
        self::RIGHT_APPOINTMENT_SETATTENDANCESTATUSBYTRAINER,
        self::RIGHT_APPOINTMENT_UPDATE,

        self::RIGHT_PROFILE_ADDNEWTEAM,
        self::RIGHT_PROFILE_CHANGE,
        self::RIGHT_PROFILE_DELETE,
        self::RIGHT_PROFILE_DELETEBYTRAINER,
        self::RIGHT_PROFILE_GET,
        self::RIGHT_PROFILE_GETALL,
        self::RIGHT_PROFILE_UPDATE,
        self::RIGHT_PROFILE_UPDATEPIC,
        self::RIGHT_PROFILE_UPDATEPICBYTRAINER,
        self::RIGHT_PROFILE_UPDATEBYTRAINER,
        self::RIGHT_PROFILE_GETCACHEUPDATE,

        self::RIGHT_TEAM_DELETEINVITATION,
        self::RIGHT_TEAM_INVITEPLAYER,
        self::RIGHT_TEAM_LOAD,
        self::RIGHT_TEAM_RESENDINVITATION,
        self::RIGHT_TEAM_UPDATE,
        self::RIGHT_TEAM_UPDATEEMBLEM,
        self::RIGHT_TEAM_GETCURRENT,
        self::RIGHT_TEAM_CREATEPREMIUM,

        self::RIGHT_SETTINGS_GETBILLING,
        self::RIGHT_SETTINGS_GETBILLINGDETAIL,
        self::RIGHT_SETTINGS_GETPREMIUMDETAILS,
        self::RIGHT_SETTINGS_GETPREMIUMDETAIL,
        self::RIGHT_SETTINGS_UPDATEPREMIUMDETAIL,
        self::RIGHT_SETTINGS_CANCELSUBSCRIPTION,

        self::RIGHT_STATS_DASHBOARD,

        self::RIGHT_ACCOUNT_CHANGEEMAIL,
        self::RIGHT_ACCOUNT_CHANGEPASSWORD,
        self::RIGHT_ACCOUNT_CONFIRMEMAILCHANGE,
        self::RIGHT_ACCOUNT_GETEMAILCHANGE,
        self::RIGHT_ACCOUNT_REGISTERDEVICE,

        self::RIGHT_CONTENT_REQUESTSUPPORT
    );

    // Player
    protected static $PLAYER = array(
        self::RIGHT_NEWS_GET,
        self::RIGHT_NEWS_GETUNREADCOUNT,
        self::RIGHT_NEWS_CREATECHAT,

        self::RIGHT_APPOINTMENT_SUBSCRIBE,
        self::RIGHT_APPOINTMENT_GETDETAIL,
        self::RIGHT_APPOINTMENT_GETLIST,
        self::RIGHT_APPOINTMENT_SETATTENDANCESTATUS,

        self::RIGHT_PROFILE_ADDNEWTEAM,
        self::RIGHT_PROFILE_CHANGE,
        self::RIGHT_PROFILE_DELETE,
        self::RIGHT_PROFILE_GET,
        self::RIGHT_PROFILE_GETALL,
        self::RIGHT_PROFILE_UPDATE,
        self::RIGHT_PROFILE_UPDATEPIC,
        self::RIGHT_PROFILE_GETCACHEUPDATE,

        self::RIGHT_TEAM_LOAD,
        self::RIGHT_TEAM_GETCURRENT,
        self::RIGHT_TEAM_CREATEPREMIUM,

        self::RIGHT_SETTINGS_GETBILLING,
        self::RIGHT_SETTINGS_GETBILLINGDETAIL,
        self::RIGHT_SETTINGS_GETPREMIUMDETAILS,
        self::RIGHT_SETTINGS_GETPREMIUMDETAIL,
        self::RIGHT_SETTINGS_UPDATEPREMIUMDETAIL,
        self::RIGHT_SETTINGS_CANCELSUBSCRIPTION,

        self::RIGHT_STATS_DASHBOARD,

        self::RIGHT_ACCOUNT_CHANGEEMAIL,
        self::RIGHT_ACCOUNT_CHANGEPASSWORD,
        self::RIGHT_ACCOUNT_CONFIRMEMAILCHANGE,
        self::RIGHT_ACCOUNT_GETEMAILCHANGE,
        self::RIGHT_ACCOUNT_REGISTERDEVICE,

        self::RIGHT_CONTENT_REQUESTSUPPORT
    );

    // Fan, Passiv
    protected static $FAN = array(
        self::RIGHT_NEWS_GET,
        self::RIGHT_NEWS_GETUNREADCOUNT,
        self::RIGHT_NEWS_CREATECHAT,

        self::RIGHT_APPOINTMENT_SUBSCRIBE,
        self::RIGHT_APPOINTMENT_GETDETAIL,
        self::RIGHT_APPOINTMENT_GETLIST,

        self::RIGHT_PROFILE_ADDNEWTEAM,
        self::RIGHT_PROFILE_CHANGE,
        self::RIGHT_PROFILE_DELETE,
        self::RIGHT_PROFILE_GET,
        self::RIGHT_PROFILE_GETALL,
        self::RIGHT_PROFILE_UPDATE,
        self::RIGHT_PROFILE_UPDATEPIC,
        self::RIGHT_PROFILE_GETCACHEUPDATE,

        self::RIGHT_TEAM_LOAD,
        self::RIGHT_TEAM_GETCURRENT,
        self::RIGHT_TEAM_CREATEPREMIUM,

        self::RIGHT_SETTINGS_GETBILLING,
        self::RIGHT_SETTINGS_GETBILLINGDETAIL,
        self::RIGHT_SETTINGS_GETPREMIUMDETAILS,
        self::RIGHT_SETTINGS_GETPREMIUMDETAIL,
        self::RIGHT_SETTINGS_UPDATEPREMIUMDETAIL,
        self::RIGHT_SETTINGS_CANCELSUBSCRIPTION,

        self::RIGHT_STATS_DASHBOARD,

        self::RIGHT_ACCOUNT_CHANGEEMAIL,
        self::RIGHT_ACCOUNT_CHANGEPASSWORD,
        self::RIGHT_ACCOUNT_CONFIRMEMAILCHANGE,
        self::RIGHT_ACCOUNT_GETEMAILCHANGE,
        self::RIGHT_ACCOUNT_REGISTERDEVICE,

        self::RIGHT_CONTENT_REQUESTSUPPORT
    );

    /**
     * @param TB_Shared_Ent_TeamData_Profile $profile
     * @param TB_Server_Core_Request $request
     * @return bool
     */
    public static function hasRight( TB_Shared_Ent_TeamData_Team $team, TB_Shared_Ent_TeamData_Profile $profile, TB_Server_Core_Request $request ) {
        // Get dynamic props
        $role = strtoupper( $profile->role );
        $reqRight = strtoupper( $request->controller ) . '-' . strtoupper( $request->action );

        if ( in_array( $reqRight, self::$PREMIUM ) && false === $team->isPremium() )
        {
            TB_Server_Utils_Log::get()->log( 'Team is not premium and tries to do: ' . $reqRight );
            return false;
        }

        // Return check
        return ( in_array( $reqRight, self::$$role ) );
    }
} 