Home Reference Source

application/current-user.js

import Immutable from 'immutable';

/**
 * Handle the currently logged ino user.
 * @type {Object}
 */
const CurrentUser = {

    /**
     * Read the userdata as provided by the framework. Data is base64 encoded.
     * Once data is read, the original variable is nullified to avoid tampering
     * @param  {string} str Base64 encoded string
     */
    init(str, acls = '') {
        if (!str) {
            return;
        }

        const decoded = atob(str.substr(10));
        const decodedAcl = atob(acls);

        this.userData = Immutable.fromJS(JSON.parse(decoded));
        this.aclData = Immutable.fromJS(JSON.parse(decodedAcl));
    },

    /**
     * Get user id
     * @return {string}
     */
    id() {
        return this.userData.get('id');
    },
    getId() {
        return this.id();
    },

    /**
     * Get user SRN
     * @return {string}
     */
    srn() {
        return this.userData.get('srn');
    },
    getSrn() {
        return this.srn();
    },

    /**
     * Get user UUID
     * @return {string}
     */
    uuid() {
        return this.userData.get('uuid');
    },
    getUuid() {
        return this.uuid();
    },

    /**
     * Get user name
     * @return {string}
     */
    name() {
        return this.userData.get('name');
    },
    getName() {
        return this.name();
    },
    getFirstName() {
        let str = this.name().split(' ');

        return str[0];
    },

    /**
     * Get user email
     * @return {string}
     */
    email() {
        return this.userData.get('email');
    },
    getEmail() {
        return this.email();
    },

    /**
     * Get user API private token
     * @return {string}
     */
    privateToken() {
        return this.userData.get('private_key');
    },
    getPrivateToken() {
        return this.privateToken();
    },

    /**
     * Get user API public token
     * @return {string}
     */
    publicToken() {
        return this.userData.get('public_key');
    },
    getPublicToken() {
        return this.publicToken();
    },

    /**
     * Get user Gravatar URL
     * @return {string}
     */
    gravatar() {
        return this.userData.get('gravatar');
    },
    getGravatar() {
        return this.gravatar();
    },

    /**
     * Is SNworks user?
     * @return {boolean}
     */
    isSnworks() {
        return parseInt(this.userData.get('is_snworks')) ? true : false;
    },

    getRoles() {
        return this.userData.get('roles');
    },

    hasRole(role) {
        const roles = this.userData.get('roles');
        const found = roles.find((r) => r.get('name') == role);
        if (found && found.size) {
            return true;
        }

        return false;
    },

    /**
     * Allows you to check the ACL status of the current user
     * against an action. This is a fully synchronous action.
     * <pre>
     * CurrentUser.isAllowed('Content', 'delete');
     * </pre>
     * @param {string} controller
     * @param {string} action
     * @return {boolean}
     */
    isAllowed(controller, action) {
        // basically loop over groups, and check if control is
        // allowed or starred
        const roles = this.userData.get('roles');
        let isAllowed = false;
        for (let i=0; i<roles.size; i++) {
            let role = roles.get(i);

            let acl = this.aclData.get(role.get('name'));
            if (!acl || !acl.size) {
                continue;
            }

            let aclRole = acl.get(controller);
            if (!aclRole || !aclRole.size) {
                continue;
            }

            for (let j=0; j<aclRole.size; j++) {
                let aclAction = aclRole.get(j);

                if ((aclAction.get('action') == '*' || aclAction.get('action') == action)
                    && parseInt(aclAction.get('allowed'))) {

                    isAllowed = true;
                    break;
                }
            }

            if (isAllowed) {
                // break the loop;
                break;
            }
        }

        console.log('IS allowed', isAllowed);
        return isAllowed;
    }

};

// initialize the data and wipe the original var
const data = window._app_udata ? window._app_udata : false;
const acls = window._acl ? window._acl : false;
CurrentUser.init(data, acls);
window._app_udata = false;
window._acl = false;

export default CurrentUser;