lundi 2 mars 2015

How to verify if current user is a SCA (Site Collection Administrator)?



I have a NavBar at my master page that will only be displayed to the SCA (Site Collection Administrators). I'm using jquery SPServices to do that validation, but the only examples that i found suggest to do this by validating the users group. And with some more research, I also discovered that the SCA aren't a group.



$(document).ready( function ()
{
var isUserOnly = false; //User group member only
var isAdmin=false;
var ADMINGROUP = "Administradores de conjunto de sites"; // SPGroup named “Admin”

$().SPServices
({
operation: "GetGroupCollectionFromUser", //Calling GetGroupCollectionFromUser.asmx web service
userLoginName: $().SPServices.SPGetCurrentUser(), //Calling Current User
async: false,
completefunc: function(xData, Status)
{ $(xData.responseXML).find("Groups").each(function() //Retrieving the values from Group method of Web Service
{
$(this).find("Group").each(function() //Parsing through all the groups the current user belongs to
{
//Checking whether Current User belongs to Admin Group
if(($(this).attr("Name").toLowerCase() === ADMINGROUP.toLowerCase()))
isAdmin = true;

//If Current User does not belong to Admin, then definitely a normal user
else
isUserOnly=true;
});
});
}
}); // Closing SPServices Method
if(!isAdmin){document.getElementById('sideNavBox').style.visibility = 'hidden';}
});`




I Also found this one, but i'm not quite sure how to use it (i tried):



function isCurrentUserSiteAdmin(OnSuccess,OnError)
{
var context = SP.ClientContext.get_current();
var user = context.get_web().get_currentUser();
context.load(user);

context.executeQueryAsync(
function() {
var isSiteAdmin = user.get_isSiteAdmin();
OnSuccess(isSiteAdmin);
},
OnError
);
}

//Usage
isCurrentUserSiteAdmin(function(isAdmin){
console.log(isAdmin);
},function(sender,args){
console.log('An error occured: ' + args.get_message());
}
);


Any help is welcome! Thanks in advance.


P.S: if you know another way, let me know too!








0 commentaires:

Enregistrer un commentaire