reactjssharepointsharepoint-onlinespfxpnp-js

Getting nested site users in SPFX


I need to find whether current user is a site owner in spfx react . Please find below code which I am using right now

 const ownerGroupId = (await sp.web.associatedOwnerGroup()).Id
    const users = await sp.web.siteGroups.getById(ownerGroupId).users();
    await sp.web.currentUser.get().then(async(user) => {
if(users.some(r=>r.Id==user.Id))
{
console.log("Site Owner")
}
});

But problem I am facing is in site owner groups is having groups which current user is part of. I need to compare the users who are nested inside site owner groups. any helps will be highly appreciated


Solution

  • What about this approach:

    web.getCurrentUserEffectivePermissions().then((permissions)=>{
       if(web.hasPermissions(permissions,PermissionKind.ManageWeb) && web.hasPermissions(permissions,PermissionKind.ManagePermissions)&& web.hasPermissions(permissions,PermissionKind.CreateGroups))
       {
         console.log("User is site owner");
       }
       else
       {
         console.log("User is not site owner");
       }
    

    Or

    const currentUser = await sp.web.currentUser.get();
    const response = await this.context.spHttpClient.get(`${this.context.pageContext.web.absoluteUrl}/_api/site/owner`, SPHttpClient.configurations.v1);
    const siteOwner: any = await response.json();
    let siteOwner: boolean = userData ? userData.Id === currentUser.Id ? true : false: false;