Forum Discussion

igor_nadiein's avatar
4 years ago

Default bio widget doen't work for plain Users and can't parse it from DB for plain Users

Hi folks. Need the help of the community brain. Found out that the default user bio widget, which parse User description from the settings doesn't work for plain Users except of Admins.

So I decided to write my own component to parse that info from DB.

Here is the code:

 

<#include "theme-lib.common-variables.ftl" />

<#if user.registered >
    <#assign qry = "SELECT biography FROM users WHERE id = '${user.id}'"?url />
    <#assign res = rest('2.0', '/search?q=${qry}') />
</#if>

<@liaAddScript>
    ;(function() {
        const messageElement = document.querySelector('.lia-component-article');

        if (!messageElement) return;

        <#if user.registered>
            <#if ( res.status?matches('success') && (res.data.size > 0) )>
                <#assign output = res.data.items[0].biography?esc />
                const selectors = {
                    main: '.lia-quilt-row-main',
                    wrapper: '.lia-quilt-column-alley-single',
                    ref: '#labelsForMessage',
                    bio: 'custom-widget-user-bios'
                };

                const parent = messageElement.querySelector(selectors.main + ' ' + selectors.wrapper);
                const refEl = messageElement.querySelector(selectors.ref);

                let p = document.createElement('p');
                p.classList.add(selectors.bio);
                p.textContent = '${output}';
                console.log(messageElement, refEl)
                parent.insertBefore(p, refEl);
            </#if>
        </#if>
    }());
</@liaAddScript>

 

 

It Does work with Admins, but again,  does not work with plain Users somewhy. It throw an exception in the console:

 

<div style="font-style: italic; font-weight: lighter; color: darkGrey; background-color: Beige; padding: 10px;" class="lia-widget-not-found">
	This widget could not be displayed.
</div>

 

 

Help me pls, what's going wrong with that user bio widget and Data i wanna parse it to the view by myself.

Also I didn't find any flag in the admin that could representatively control that feature and enable/disable it.

3 Replies

  • Hi @igo

    The user biography is a permission driven setting which can be enabled or disabled by user from My Settings -> Preferences -> Privacy -> Show private information to URL: (/t5/user/myprofilepage/tab/user-preferences:privacy) Please see the attached screenshot for your reference. You need to set it to "All" in order to see it for non-admin users as well.

    If you need to show it in all conditions (non-admin users) consider using restadmin calls to fetch the user biography in your customization. Though it is not recommended to use excesive Restadmin calls as it effects the performance of the application and can cause security vulnerability.

    Thanks

  • igor_nadiein's avatar
    igor_nadiein
    Guide
    4 years ago

    But with restadmin request I receive the same error exception in the console "This widget could not be displayed."

    Why it complains on Widget if I'm requesting data from DB, not the widget?

    So I wonder, is there a way to show that author bio in the Articles for Members, who are watching that Article without Author given permission for that?

    Sounds for me that we do need to request from DB the list of all users and set the setting flag to TRUE to show that bio for everybody.

  • TysonN's avatar
    TysonN
    Khoros Alumni (Retired)
    4 years ago

    him_varma is correct. 

    Your code is probably throwing a freemarker error for a non-admin user because one of your freemarker objects doesn't exist for a non-admin user.  And of course the error message for a non-admin user won't include any useful debugging info.

    The component might fail more gracefully if you add an additional check like so:

     

    <#if (res.data.items[0].biography)?has_content>...</#if>

     

    This will check for both not null and not empty.  However, it won't address the issue of some profile fields not being retrievable for non-admin users via a rest() call.  This may be an acceptable use case for restadmin().