Kode
Kode

Reputation: 3215

Using AngularJS Service to Check if User has Admin Permissions

I am building an SharePoint App using AngularJS and am attempting to define a service that retrieves if the user is an Admin or not. The service itself is successfully logging/working as expected, but I am not sure how to use this in a controller. My end goal is that when a page loads that is tied to a controller, that this service checks if they are an admin or not. From that point, I can do all sorts of magic (ex. redirect, etc.). Here is my service:

    // Check if user is an admin
appServices.factory('appAdminCheck', ['$resource', 'appCurrentUserProfile', 'appAdmins', function ($resource, appCurrentUserProfile, appAdmins) {
    var userAdmin = [];

    appCurrentUserProfile.query(function (usercheck) {
        var userID = usercheck.Id;

        appAdmins.query(function (admins) {
            var admins = admins.value; // Data is within an object of "value", so this pushes the server side array into the $scope array

            // Foreach type, push values into types array
            angular.forEach(admins, function (adminvalue, adminkey) {
                if (adminvalue.Admin_x0020_NameId == userID) {
                    userAdmin = true;
                    console.log("I'm an Admin" + userAdmin);
                }
            });
        });
    });
    return userAdmin;
}]);

Update: Upon closer inspection, I would like to return the array of values, but it keeps stating that the array length is 0. I am sure it is because I am not "returning" properly.

Here is my updated service:

    appServices.factory('appAdminCheck', ['$resource', 'appCurrentUserProfile', 'appAdmins', function ($resource, appCurrentUserProfile, appAdmins) {

    var userAdmin = [];

    var checkUser = function() {
    appCurrentUserProfile.query(function (usercheck) {
        var userID = usercheck.Id;

        appAdmins.query(function (admins) {
            var admins = admins.value; // Data is within an object of "value", so this pushes the server side array into the $scope array

            // Foreach type, push values into types array
            angular.forEach(admins, function (adminvalue, adminkey) {
                if (adminvalue.Admin_x0020_NameId == userID) {

                    userAdmin.push({
                        isAdmin: 'Yes',
                        role: adminvalue.Role,
                    });

                }
            });
        });
    });
    return userAdmin;
    }

    return {
        checkUser: checkUser
    };
}]);

Here is a logging call in a controller:

var test = appAdminCheck.checkUser();
console.log(test);

Upvotes: 0

Views: 1331

Answers (1)

Phil
Phil

Reputation: 164767

Seeing as there appears to be some asynchronous actions happening, you'll want to return a promise. You can do this by chaining the then promise resolution callbacks from your other services (assuming they're $resource instances or similar). For example...

appServices.factory('appAdminCheck', function (appCurrentUserProfile, appAdmins) {
    return function() {
        return appCurrentUserProfile.query().$promise.then(function(usercheck) {
            return appAdmins.query().$promise.then(function(admins) {
                // this needs to change if admins.value is not an array
                for (var i = 0, l = admins.value.length; i < l; i++) {
                    if (admins.value[i].Admin_x0020_NameId === usercheck.Id) {
                        return true;
                    }
                }
                return false;
            });
        });
    };
});

Then, you can use this promise resolution in your controller, eg

appAdminCheck().then(function(isAdmin) {
    // isAdmin is true or false
});

Upvotes: 2

Related Questions