How do I get a users fullname in Volto

In the following example I get the fullname of the author of an item using the action getUser(userId) to show it in the view. That works fine but only if I am logged in. How do I show it for anonymous users as well?

And another question: How do I find out that the user-data will end up in state.users.user and not in state.user or in state.deadparrot?

/**
 * NewsItemView view component.
 * @module components/theme/View/NewsItemView
 */

import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import RenderBlocks from '@plone/volto/components/theme/View/RenderBlocks';
import config from '@plone/volto/registry';
import { getUser } from '@plone/volto/actions';
import { useDispatch, useSelector } from 'react-redux';

/**
 * NewsItemView view component class.
 * @function NewsItemView
 * @params {object} content Content object.
 * @returns {string} Markup of the component.
 */
const NewsItemView = ({ content }) => {
  const Container = config.getComponent({ name: 'Container' }).component;
  const dispatch = useDispatch();
  const userId = content?.creators ? content.creators[0] : '';
  const user = useSelector((state) => state.users.user);
  useEffect(() => {
    dispatch(getUser(userId));
  }, [dispatch, userId]);

  return (
    <Container id="page-document" className="view-wrapper newsitem-view">
      {userId && <div className="author">{user?.fullname || userId}</div>}
      <RenderBlocks content={content} />
    </Container>
  );
};

/**
 * Property types.
 * @property {Object} propTypes Property types.
 * @static
 */
NewsItemView.propTypes = {
  content: PropTypes.shape({
    title: PropTypes.string,
    description: PropTypes.string,
    text: PropTypes.shape({
      data: PropTypes.string,
    }),
  }).isRequired,
};

export default NewsItemView;

(Answering second question) I think this is the place where that info is written to the store:

It's the users reducer, so it's state.users. The reducer functions and their names are declared here:

based on tracking this from here:

3 Likes

I found the answer to the first question. The permission plone.restapi: Access Plone user information is used in the @users endpoint that is used by the action getUser. By default that is only granted to Manager (not even Site Administrator).
If I grant that permission to Anonymous it works.

Doing that can obviously be a security-problem depending on your use-case.
I will probably overwrite the endpoint to only return fullname and home_page in case the user is not a Manager.

2 Likes

Interesting... I'm facing a similar situation, except, I want to check for the user's roles.
I don't want to give anonymous that much control of the @users endpoint.
Perhaps I need a simple @myroles endpoint that reports a user's current roles