Skip to content

Commit

Permalink
some polishment (renaming classes, some new methods etc.)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgoldhammer committed Nov 2, 2016
1 parent 40a7e04 commit b30c7e6
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 31 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/target
/alf_data_dev
/alf_data_dev**
.idea/**
alfresco.log
share.log
solr.log
alfresco.log.*
share.log.*
solr.log.*
@@ALFRESCO_SOLR4_DATA_DIR@@/

**.iml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
package de.jgoldhammer.alfresco.jscript.favorites;

import com.google.common.base.Preconditions;
import de.jgoldhammer.alfresco.jscript.jobs.ScriptJob;
import org.alfresco.query.PagingRequest;
import org.alfresco.query.PagingResults;
import org.alfresco.repo.favourites.PersonFavourite;
import org.alfresco.repo.jscript.BaseScopableProcessorExtension;
import org.alfresco.repo.jscript.ScriptNode;
import org.alfresco.repo.processor.BaseProcessorExtension;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.dictionary.DictionaryService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.google.common.base.Preconditions;
import org.alfresco.repo.jscript.ScriptNode;
import org.alfresco.repo.model.filefolder.HiddenAspect;
import org.alfresco.repo.model.filefolder.HiddenFileInfo;
import org.alfresco.service.cmr.repository.NodeRef;

/**
* wraps the api to hide nodes for certain clients or completely...
Expand All @@ -20,5 +22,28 @@ public void hideNodeExplicit(ScriptNode scriptNode){
hiddenAspect.hideNodeExplicit(scriptNode.getNodeRef());
}

public void unhideNode(ScriptNode scriptNode){
Preconditions.checkNotNull(scriptNode);
hiddenAspect.unhideExplicit(scriptNode.getNodeRef());
}

public boolean hasHiddenAspect(ScriptNode scriptNode){
Preconditions.checkNotNull(scriptNode);
return hiddenAspect.hasHiddenAspect(scriptNode.getNodeRef());
}

public void removeHiddenAspect(ScriptNode scriptNode){
Preconditions.checkNotNull(scriptNode);
hiddenAspect.removeHiddenAspect(scriptNode.getNodeRef());
}

public HiddenFileInfo onHiddenPath(ScriptNode scriptNode){
Preconditions.checkNotNull(scriptNode);
return hiddenAspect.onHiddenPath(scriptNode.getNodeRef());
}





}
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package de.jgoldhammer.alfresco.jscript.messages;

import java.util.List;
import java.util.Set;

import com.google.common.base.Preconditions;
import org.alfresco.repo.i18n.MessageService;
import org.alfresco.repo.jscript.BaseScopableProcessorExtension;
import org.alfresco.repo.jscript.ValueConverter;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

/**
*
*
* copied from https://github.com/bluedolmen/App-blue-courrier/blob/ba0fa1e8119419fdb3bb3ccf844e60ad9df9a736/alfresco-extensions/src/main/java/org/bluedolmen/repo/jscript/MessageScript.java
*/
public class MessageScript extends BaseScopableProcessorExtension {
public class ScriptMessageService extends BaseScopableProcessorExtension {

private final ValueConverter valueConverter = new ValueConverter();

Expand All @@ -31,13 +34,19 @@ public String get(String messageKey, Scriptable params) {
public String getMessage(String messageKey, Scriptable params) {

final Object convertedValue = valueConverter.convertValueForJava(params);
if (!(convertedValue instanceof List<?>)) return null;
Preconditions.checkArgument(convertedValue instanceof List<?>);

final List<?> paramsList = (List<?>) convertedValue;
final Object[] paramsArray = paramsList.toArray();
return messageService.getMessage(messageKey, paramsArray);

}

public Scriptable getRegisteredBundles(){
Set<String> registeredBundles = messageService.getRegisteredBundles();
return Context.getCurrentContext().newArray(getScope(),registeredBundles.toArray(new Object[registeredBundles.size()]));

}

// IoC

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package de.jgoldhammer.alfresco.jscript.model;

import org.alfresco.query.PagingRequest;
import org.alfresco.repo.jscript.BaseScopableProcessorExtension;
import org.alfresco.repo.jscript.ScriptNode;
import org.alfresco.repo.processor.BaseProcessorExtension;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.dictionary.CustomModelDefinition;
import org.alfresco.service.cmr.dictionary.CustomModelService;
Expand All @@ -15,19 +17,10 @@
/**
* Created by jgoldhammer on 17.09.16.
*/
public class ScriptModelService extends BaseProcessorExtension {
public class ScriptModelService extends BaseScopableProcessorExtension {

CustomModelService customModelService;
ServiceRegistry serviceRegistry;
Scriptable scope;

/**
* @see org.alfresco.repo.jscript.Scopeable#setScope(org.mozilla.javascript.Scriptable)
*/
public void setScope(Scriptable scope)
{
this.scope = scope;
}

public void setServiceRegistry(ServiceRegistry serviceRegistry) {
this.serviceRegistry = serviceRegistry;
Expand All @@ -37,6 +30,14 @@ public void setCustomModelService(CustomModelService customModelService) {
this.customModelService = customModelService;
}

/**
* checks whether the current user is a model admin...
* @return true if the user is a model admin or super admin, false if not
*/
public boolean isModelAdmin(){
return customModelService.isModelAdmin(AuthenticationUtil.getRunAsUser());
}

/**
* activates a custom model by name
*
Expand Down Expand Up @@ -65,7 +66,8 @@ public ScriptNode getModelNode(String model){

public Scriptable getCustomModels(int start, int end){
List<CustomModelDefinition> customModels = customModelService.getCustomModels(new PagingRequest(start, end)).getPage();
return Context.getCurrentContext().newArray(scope,customModels.toArray(new Object[customModels.size()]));
return Context.getCurrentContext().newArray(getScope(),customModels.toArray(new Object[customModels.size()]));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@

import com.google.common.base.Preconditions;
import org.alfresco.repo.jscript.BaseScopableProcessorExtension;
import org.alfresco.repo.jscript.People;
import org.alfresco.repo.jscript.ScriptNode;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.security.AccessPermission;
import org.alfresco.service.cmr.security.AccessStatus;
import org.alfresco.service.cmr.security.PermissionService;
import org.apache.commons.lang3.StringUtils;
import org.jbpm.graph.action.Script;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
Expand All @@ -47,26 +47,49 @@ public class ScriptPermissionService extends BaseScopableProcessorExtension {
private RetryingTransactionHelper retryingTransactionHelper;


public boolean hasReadPermission(String nodeRef){
public boolean hasReadPermission(String nodeRef) {
Preconditions.checkNotNull(nodeRef);
return permissionService.hasReadPermission(new NodeRef(nodeRef))==AccessStatus.ALLOWED;
return permissionService.hasReadPermission(new NodeRef(nodeRef)) == AccessStatus.ALLOWED;
}

public boolean hasPermission(String nodeRef, String permission){
public boolean hasPermission(String nodeRef, String permission) {
Preconditions.checkNotNull(nodeRef);
Preconditions.checkNotNull(permission);
return permissionService.hasPermission(new NodeRef(nodeRef), permission)==AccessStatus.ALLOWED;
return permissionService.hasPermission(new NodeRef(nodeRef), permission) == AccessStatus.ALLOWED;
}

public boolean hasPermission(String nodeRef, String permission, String authority){
public boolean hasPermission(String nodeRef, String permission, String authority) {
Preconditions.checkNotNull(nodeRef);
Preconditions.checkNotNull(permission);
Preconditions.checkNotNull(authority);

return AuthenticationUtil.runAs(() ->
permissionService.hasPermission(new NodeRef(nodeRef), permission) == AccessStatus.ALLOWED, authority);
return AuthenticationUtil.runAs(() ->
permissionService.hasPermission(new NodeRef(nodeRef), permission) == AccessStatus.ALLOWED, authority);
}

public boolean hasReadPermission(ScriptNode node) {
Preconditions.checkNotNull(node);
return permissionService.hasReadPermission(node.getNodeRef()) == AccessStatus.ALLOWED;
}

public boolean hasPermission(ScriptNode node, String permission) {
Preconditions.checkNotNull(node);
Preconditions.checkNotNull(permission);
return permissionService.hasPermission(node.getNodeRef(), permission) == AccessStatus.ALLOWED;
}


public boolean hasPermission(ScriptNode node, String permission, ScriptNode authority) {
Preconditions.checkNotNull(node);
Preconditions.checkNotNull(permission);
Preconditions.checkNotNull(authority);

return AuthenticationUtil.runAs(() ->
permissionService.hasPermission(node.getNodeRef(), permission) == AccessStatus.ALLOWED, (String) authority.getProperties().get("userName"));
}



/**
* sets a new permission for a certain authority on a certain node by specifying if the permission is allowed or denied.
*
Expand Down Expand Up @@ -94,6 +117,43 @@ public void setPermission(
);
}

/**
* get the permissions of a node as native javascript array of type AccessPermission
*
* @param nodeRef the node to get the permissions for
*/
public Scriptable getPermissions(String nodeRef){
Preconditions.checkNotNull(nodeRef);
Set<AccessPermission> permissions = permissionService.getPermissions(new NodeRef(nodeRef));
Object[] permissionsArray = permissions.toArray(new Object[permissions.size()]);

return Context.getCurrentContext().newArray(getScope(), permissionsArray);

}

/**
* get the current user permissions of a node as native javascript array of type AccessPermission
*
* @param node the scriptnode
*/
public Scriptable getPermissionsOfCurrentUser(ScriptNode node){
Preconditions.checkNotNull(node);
return getPermissions(node.getNodeRef().toString());
}

/**
* get all permissions of a node as native javascript array of type AccessPermission
*
* @param node the scriptnode
*/
public Scriptable getAllPermissions(ScriptNode node){
Preconditions.checkNotNull(node);
Set<AccessPermission> permissions = permissionService.getAllSetPermissions(node.getNodeRef());

Object[] permissionsArray = permissions.toArray(new Object[permissions.size()]);
return Context.getCurrentContext().newArray(getScope(), permissionsArray);
}

/**
* delete a certain permission for a certain authority on a certain node.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.jgoldhammer.alfresco.jscript.globalproperties;
package de.jgoldhammer.alfresco.jscript.variables;

import java.util.Properties;
import org.alfresco.repo.jscript.BaseScopableProcessorExtension;
Expand All @@ -7,7 +7,7 @@
* Expose global properties to js scripts
*
*/
public class ScriptGlobalProperties extends BaseScopableProcessorExtension {
public class ScriptVariablesService extends BaseScopableProcessorExtension {

private Properties properties;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@

</bean>

<bean id="globalProperties" parent="baseJavaScriptExtension" class="de.jgoldhammer.alfresco.jscript.globalproperties.ScriptGlobalProperties">
<bean id="scriptVariablesService" parent="baseJavaScriptExtension" class="de.jgoldhammer.alfresco.jscript.variables.ScriptVariablesService">
<property name="properties" ref="global-properties" />
<property name="extensionName" value="properties" />
</bean>
Expand Down

0 comments on commit b30c7e6

Please sign in to comment.