Custom Form Controls Based on User Role/Group

cancel
Showing results for 
Search instead for 
Did you mean: 
pedwards99
Partner

Custom Form Controls Based on User Role/Group

Hi there,

I'm trying to create a custom Share property form (for a workflow task) where the requirement for one of the properties is that it must only be editable by members of the Site Manager group i.e if the user is a member of the Site Manager group I want to display a text field, otherwise just a label.

I had hoped that I'd be able to get at the group membership info for the FTL for the control but I'm not sure if this is possible

For example, this does what I want for the admin user:

<#if user.isAdmin>

 ...display the text field

<#else>

...display the label

</#if>

what I need is to replace <#if user.isAdmin> with something like <#if user.isSiteManager>

Any ideas?

Thanks

Paul

9 Replies
angelborroy
Alfresco Employee

Re: Custom Form Controls Based on User Role/Group

How about invoking Alfresco Repo Rest API from control FTL to check if user is Site Manager?

Hyland Developer Evangelist
pedwards99
Partner

Re: Custom Form Controls Based on User Role/Group

Thanks Angel, I was actually just looking into how that could be done when you replied.

Have you ever come across any posts with examples?

Paul

angelborroy
Alfresco Employee

Re: Custom Form Controls Based on User Role/Group

pedwards99
Partner

Re: Custom Form Controls Based on User Role/Group

Thanks Angel.

I'm not sure if that will work directly (or maybe I'm doing something wrong). Is Alfresco.Utils.Ajax.jsonGet available in Share FTL or do I need to do something special to import it?

Paul

angelborroy
Alfresco Employee

Re: Custom Form Controls Based on User Role/Group

Probably you are missing this include:

alfresco-datalist-constraints/datalistSelectone.ftl at master · keensoft/alfresco-datalist-constrain... 

Hyland Developer Evangelist
angelborroy
Alfresco Employee

Re: Custom Form Controls Based on User Role/Group

No, it was not in that import...

You should be able to access JavaScript Object Alfresco.util.Ajax from your FTL. 

Where are you developing your extension? Is it a new form control?

Hyland Developer Evangelist
pedwards99
Partner

Re: Custom Form Controls Based on User Role/Group

Yes ! That was the problem.

I can now call the API and get the result back in Javascript. I'm guessing I now have to use javascript to construct the entire control .... or is there a way to use the javascript value in FTL, something like <#if isSiteManager> ?

pedwards99
Partner

Re: Custom Form Controls Based on User Role/Group

Ah, maybe it was working all the time and just a bug in my JS?

Its a new form control (based on textarea.ftl)

pedwards99
Partner

Re: Custom Form Controls Based on User Role/Group

Got it it working. In the end, I let FTL build the form then use JS to disable the field that I don't want edited - much easier than trying to build the form in JS.

Thanks for your help !

My code below for anyone interested. (Currently only works for one site, but that could be changed)

<#include "/org/alfresco/components/form/controls/common/utils.inc.ftl" />

<#if field.control.params.rows??><#assign rows=field.control.params.rows><#else><#assign rows=3></#if>

<#if field.control.params.columns??><#assign columns=field.control.params.columns><#else><#assign columns=60></#if>

<div class="form-field alf-textarea">

   <#if form.mode == "view">

   <div class="viewmode-field">

      <#if field.mandatory && field.value == "">

      <span class="incomplete-warning"><img src="${url.context}/res/components/form/images/warning-16.png" title="${msg("form.field.incomplete")}" /><span>

      </#if>

      <span class="viewmode-label">${field.label?html}:</span>

      <#if field.control.params.activateLinks?? && field.control.params.activateLinks == "true">

         <#assign fieldValue=field.value?html?replace("((http|ftp|https):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?\\^=%&:\\/~\\+#]*[\\w\\-\\@?\\^=%&\\/~\\+#])?)", "<a href=\"$1\" target=\"_blank\">$1</a>", "r")>

      <#else>

         <#assign fieldValue=field.value?html>

      </#if>

      <span class="viewmode-value"><#if fieldValue == "">${msg("form.control.novalue")}<#else>${fieldValue}</#if></span>

   </div>

   <#else>

  

     <script type="text/javascript">//<![CDATA[

     

      Alfresco.util.Ajax.jsonGet({  

          url: encodeURI(Alfresco.constants.PROXY_URI + "/api/sites/my-site/memberships/${user.name}"),

          successCallback:

          {

             fn: function loadWebscript_successCallback(response, config)

             {

                 var obj = eval('(' + response.serverResponse.responseText + ')');

                

                 if (obj)

                 {

          var isSiteManager = (obj.role == "SiteManager");

             

                    if (!isSiteManager) {

                    var textarea = document.getElementById("${fieldHtmlId}");

                    textarea.disabled=true;

                    }

                 }

             }

          },

          failureCallback: {

           fn: function failedToGetUserMembership(response, config)

             {

window.alert("Error getting user role from Site. Change Note Title will be disabled.");

var textarea = document.getElementById("${fieldHtmlId}");

                textarea.disabled=true;

             }

          }

     });

     

     //]]></script>

     

   <label for="${fieldHtmlId}">${field.label?html}:<#if field.mandatory><span class="mandatory-indicator">${msg("form.required.fields.marker")}</span></#if></label>

   <@formLib.renderFieldHelp field=field />

   <textarea id="${fieldHtmlId}" name="${field.name}" rows="${rows}" cols="${columns}" tabindex="0"

      <#if field.description??>title="${field.description}"</#if>

      <#if field.control.params.styleClass??>class="${field.control.params.styleClass}"</#if>

      <#if field.control.params.style??>style="${field.control.params.style}"</#if>

      <#if field.control.params.maxLength??>maxlength="${field.control.params.maxLength}"<#else>maxlength="1024"</#if>

      <#if field.disabled && !(field.control.params.forceEditable?? && field.control.params.forceEditable == "true")>disabled="true"</#if>>${field.value?html}</textarea>

   </#if>

</div>