Current logged in user and permissions

1276
3
Jump to solution
01-11-2023 10:48 AM
Labels (2)
MatthewClockel
New Contributor

I am building a custom widget that recognizes the logged in user so this can be applied to the UI.  My goal is for the application to recognize the current logged in user and display information on the tables and pages that pertains to the particular user.  So far I was able to create a custom widget that displays the username of the logged in user.    Any one have any experience with this and can help develop this.  This would be very useful for Esri experience builder users.  

 

So far this is the code for the custom widget that displays the current user.  This is the widget.tsx file.

 

/** @jsx jsx */
import lang from "esri/core/lang";
import Credential from "esri/identity/Credential";
import IdentityManager from "esri/identity/IdentityManager";
import { React, AllWidgetProps, jsx, portalUtils, SessionManager } from "jimu-core";
import { getAppStore } from 'jimu-core';
import { userInfo } from "os";
import { userSignIn } from "../../../../../jimu-core/lib/app-actions";


export default class Widget extends React.PureComponent<AllWidgetProps<any>, any> {

    render() {
        let state = getAppStore().getState().user.username;
        return <div>{state}</div>;
    }
}

 

 

When this widget is dragged into the UI of the application, the user name is displayed. 

1 Solution

Accepted Solutions
Grant-S-Carroll
Esri Contributor

If you want all the details related to a user, and the user has logged in, then you can obtain these details by making a call to the portal/self end point. See the REST API details here:

portal/self 

This will return a detailed object for the user, including the users privileges and the groups that they are a member of.

Sample code:

   getUserDetails = (resp) => { 
let token = this.props.token;
        const userUrl = this.props.portalUrl + '/sharing/rest/community/self?token=' + token + '&f=json'

        fetch(userUrl, {            
        }).then(
            response => response.json()
        ).then((data: any) => {
            this.parseGetUserDetails(data);
        })
            // Catch any errors we hit and update the app
            .catch((error: any) => {
                console.error(error);
                return null;

            });
}

View solution in original post

3 Replies
Grant-S-Carroll
Esri Contributor

If you want all the details related to a user, and the user has logged in, then you can obtain these details by making a call to the portal/self end point. See the REST API details here:

portal/self 

This will return a detailed object for the user, including the users privileges and the groups that they are a member of.

Sample code:

   getUserDetails = (resp) => { 
let token = this.props.token;
        const userUrl = this.props.portalUrl + '/sharing/rest/community/self?token=' + token + '&f=json'

        fetch(userUrl, {            
        }).then(
            response => response.json()
        ).then((data: any) => {
            this.parseGetUserDetails(data);
        })
            // Catch any errors we hit and update the app
            .catch((error: any) => {
                console.error(error);
                return null;

            });
}
MatthewClockel
New Contributor

This is great.  Thank you so much for this information.  This is exactly what I was looking for.

0 Kudos
Ranga_Tolapi
Occasional Contributor III

I believe, user details can be fetched from 

this.props.user
0 Kudos