cancel
Showing results for 
Search instead for 
Did you mean: 

Modifying the Share Header

eswbitto
Confirmed Champ
Confirmed Champ
Hello Everyone,

I have been tasked with coming up with a solution to be able to allow a non-domain/society user be able to access files within alfresco. Essentially, I need to completely separate my regular user from this one specific user and its ability to navigate anywhere or find any information out about the rest of my sites and people. So I thought the best way to do that is to create a site and assign appropriate permissions to the document library and also remove the menu bar from the top.

I followed this link instructions…
http://blogs.alfresco.com/wp/developer/2013/09/16/customizing-the-share-header-part-3/

I haven't ever written javascript and I think I'm writing it wrong.

I've modified this file on a test server
tomcat/webapps/share/WEB-INF/classes/alfresco/site-webscripts/org/alfresco/share/header/share-header.get.js


<import resource="classpath:/alfresco/site-webscripts/org/alfresco/share/imports/share-header.lib.js">

model.jsonModel = {
   rootNodeId: "share-header",
   services: getHeaderServices(),
   widgets: [
      {
         id: "SHARE_VERTICAL_LAYOUT",
         name: "alfresco/layout/VerticalWidgets",
         config:
         {
            widgets: getHeaderModel()
         }
      }
   ]
if (user.istest1)
{
widgetUtils.deleteObjectFromArray(model.jsonModel, "id", "HEADER_MY_FILES");
widgetUtils.deleteObjectFromArray(model.jsonModel, "id", "HEADER ADMIN CONSOLE");
widgetUtils.deleteObjectFromArray(model.jsonModel, "id", "HEADER_REPOSITORY");
widgetUtils.deleteObjectFromArray(model.jsonModel, "id", "HEADER_PEOPLE");
widgetUtils.deleteObjectFromArray(model.jsonModel, "id", "HEADER_TASKS");
widgetUtils.deleteObjectFromArray(model.jsonModel, "id", "HEADER_SITES_MENU");
widgetUtils.deleteObjectFromArray(model.jsonModel, "id", "HEADER SHARE FILES");
widgetUtils.deleteObjectFromArray(model.jsonModel, "id", "HEADER_HOME");
}
};


I think I'm not doing it right. Can someone point me in the right direction?
7 REPLIES 7

eswbitto
Confirmed Champ
Confirmed Champ
Ok so the above didn't work, but I did find the work around that was outlined in a JIRA. Here is the code that I have.


var widget, widgetsToRemove = [ "HEADER_SHARED_FILES", "HEADER_MY_FILES", "HEADER_PEOPLE", "HEADER_TASKS", "HEADER_REPOSITORY",
"HEADER_SEARCH", "HEADER_SITES_MENU" ], idx, max;

if (user.istest1);

for (idx = 0, max = widgetsToRemove.length; idx < max; idx++)
{ findAndRemoveIn(model.jsonModel.widgets, null, null, widgetsToRemove[idx]); }

function findAndRemoveIn(obj, arrContext, arrIdx, id)
{
var idx, max, key;
if (obj !== undefined && obj !== null)
{
if (Object.prototype.toString.apply(obj) === "[object Object]")
{
if (obj.hasOwnProperty("id") && obj.id === id)
{
if (arrContext !== null && arrIdx !== null)
{ arrContext.splice(arrIdx, 1); }

else
{ logger.debug("Unexpected match outside of array structure: " + jsonUtils.toJSONString(obj)); }

}
else
{
for (key in obj)
{
if (obj.hasOwnProperty(key))
{ findAndRemoveIn(obj[key], null, null, id); }

}
}
}
else if (Object.prototype.toString.apply(obj) === "[object Array]")
{
for (idx = 0, max = obj.length; idx < max; idx++)
{ findAndRemoveIn(obj[idx], obj, idx, id); }

}
}
}


The only thing that I struggle with is the if condition for the user that is logged in. I want to define a specific user that this JS would apply to, but not anyone else that logs in.

ddraper
World-Class Innovator
World-Class Innovator
That workaround won't be required in any of the Alfresco Enterprise 4.2.2 and above as the issue has now been fixed. It is possible to configure the the customization to be user or group specific (in 5.x it the group information is baked into the user object but in 4.2.x you'll need to make a request to the Repository to retrieve it. However, for comparing against user information you can just query the current user object for the users name (e.g. compare against user.name). If the user (or group) condition is satisfied then you can make the changes to the model.

eswbitto
Confirmed Champ
Confirmed Champ
@ddraper

Thanks for the reply. Yeah I have been tackling this for the last week trying to get it to work. I have a noob level understanding of the operators in JS and my biggest challenge right now is to actually code what to evaluate.

I get it that I need to create an if condition that is true then the block of code to remove the headings will execute, else do nothing.

My challenge is how do I actually tell JS hey look to see if this user is logged in.

I've tried several different methods.

Trial #1:

if (user.nameistest1){ execute this block of code

Which return a 500 error that "user" is not defined.

Trial #2
I tried creating a function like so

function userlookup()
{
   model.currentUserDetails = person.properties.userName;
}
if (person.properties.usernameistest1){

That returns "person" is not defined.

So I think I'm narrowing this down, but apparently I'm missing a step somewhere. I just need help in the coding of how to actually evaluate logged in users.

eswbitto
Confirmed Champ
Confirmed Champ
I'm really struggling with this. It seems no matter what I put in there's always an error of some variable that is not defined.


var widget, widgetsToRemove = [ "HEADER_SHARED_FILES", "HEADER_MY_FILES", "HEADER_PEOPLE", "HEADER_TASKS", "HEADER_REPOSITORY",
"HEADER_SEARCH", "HEADER_SITES_MENU" ], idx, max;

for (idx = 0, max = widgetsToRemove.length; idx < max; idx++)
findAndRemoveIn(model.jsonModel.widgets, null, null, widgetsToRemove[idx]);

function findAndRemoveIn(obj, arrContext, arrIdx, id){
{
var username = getUser (String, username);
getUser(String, username);

if (username == 'test1'){
var idx, max, key;
if (obj !== undefined && obj !== null)   
{
if (Object.prototype.toString.apply(obj) === "[object Object]")
{
if (obj.hasOwnProperty("id") && obj.id === id)
{
if (arrContext !== null && arrIdx !== null)
{ arrContext.splice(arrIdx, 1); }

else
{ logger.debug("Unexpected match outside of array structure: " + jsonUtils.toJSONString(obj)); }

}
else
{
for (key in obj)
{
if (obj.hasOwnProperty(key))
{ findAndRemoveIn(obj[key], null, null, id); }

}
}
}

else if (Object.prototype.toString.apply(obj) === "[object Array]")
{
for (idx = 0, max = obj.length; idx < max; idx++)
{ findAndRemoveIn(obj[idx], obj, idx, id); }
}
}
}else {}
}
}


Do I need to create an xml file as well as a ftl file for this to work or would just this JS be enough?

ddraper
World-Class Innovator
World-Class Innovator
What is the error you're currently seeing? e.g. which variable is missing? …and no, you shouldn't need an XML file (at least not a WebScript XML file - you obviously need an XML file that defines the extension but you must have that to be hitting the errors)

eswbitto
Confirmed Champ
Confirmed Champ
I get a 500 error. Usually the error pertains to a global variable or a variable inside my function that doesn't get declared.

I don't have any xml file built at all. I didn't think I would need one.

Edit: I take that back there is a xml file, but if that's what you are referring to then yeah there is one. If your talking about an additional than no there isn't one.(keep in mind I'm basing this whole jar off of your blog)

eswbitto
Confirmed Champ
Confirmed Champ
I have this now resolved and working. Thanks to everyone in the irc channel for the help.

So I was doing the wrong approach with the code above. In order for the headers to be removed on a specific user I had to create a group and then put that user in it.

Then created an xml file with the following in it.


<?xml version="1.0"?>
<extension>
   <modules>
      <module>
         <id>Remove Share Headers</id>
         <version>1.0</version>
         <description>Changes anyone in GROUP_parents</description>
          <auto-deploy>true</auto-deploy>
          <evaluator type="group.module.evaluator">
                 <params>
                            <groups>GROUP_parents</groups>
                 </params>
          </evaluator>
          <customizations>
            <customization>
               <targetPackageRoot>org.alfresco.share.header</targetPackageRoot>
               <sourcePackageRoot>share-mod.remove-headers-link</sourcePackageRoot>
            </customization>
          </customizations>
      </module>
      </modules>
</extension>


This extension will evaluate the group "parents" and apply the JS file to it.