'use strict';

angular.module('users').factory('Permissions', ['Authentication', '$location',
	function(Authentication, $location) {
		// Permissions service logic
		// ...

		// Public API
		return {
			//check if user suits the right permissions for visiting the page, Otherwise go to 401
			userRolesContains: function (role) {
				if (!Authentication.user || Authentication.user.roles.indexOf(role) === -1) {
					return false;
				} else {
					return true;
				}
			},

            //This function returns true if the user is either admin or maintainer
            adminOrMaintainer: function () {
                if (Authentication.user && (Authentication.user.roles.indexOf('admin')> -1 || Authentication.user.roles.indexOf('maintainer')> -1)) {
                    return true;
                } else {
                    return false;
                }
            },

			isPermissionGranted: function (role) {
				if (!Authentication.user || Authentication.user.roles.indexOf(role) === -1) {
					$location.path('/401');
				}
			},

			isAdmin: function () {
				if (Authentication.user && (Authentication.user.roles.indexOf('admin') > -1)) {
					return true;
				} else {
					return false;
				}
			}
		};
	}
]);