mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-14 17:58:59 +00:00
Share Admin Console - Users tool improvements and fixes:
- First cut of CRUD operations for Users now all completed - Refactoring of UI refresh code Refactor of Person PUT REST API from Java to JavaScript implementation to match POST operation for easier future changes and fixes ETHREEOH-2067. Person PUT API now supports addition and removal of Groups from a Person. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@14267 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -0,0 +1,96 @@
|
|||||||
|
function main()
|
||||||
|
{
|
||||||
|
// Get the person details and ensure they exist for update
|
||||||
|
var userName = url.extension;
|
||||||
|
var person = people.getPerson(userName);
|
||||||
|
if (person == null)
|
||||||
|
{
|
||||||
|
status.setCode(status.STATUS_NOT_FOUND, "Person " + userName + " does not exist");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// assign new values to the person's properties
|
||||||
|
if (!json.isNull("firstName"))
|
||||||
|
{
|
||||||
|
person.properties["firstName"] = json.get("firstName");
|
||||||
|
}
|
||||||
|
if (!json.isNull("lastName"))
|
||||||
|
{
|
||||||
|
person.properties["lastName"] = json.get("lastName");
|
||||||
|
}
|
||||||
|
if (!json.isNull("email"))
|
||||||
|
{
|
||||||
|
person.properties["email"] = json.get("email");
|
||||||
|
}
|
||||||
|
if (!json.isNull("title"))
|
||||||
|
{
|
||||||
|
person.properties["title"] = json.get("title");
|
||||||
|
}
|
||||||
|
if (!json.isNull("organisation"))
|
||||||
|
{
|
||||||
|
person.properties["organization"] = json.get("organisation");
|
||||||
|
}
|
||||||
|
if (!json.isNull("jobtitle"))
|
||||||
|
{
|
||||||
|
person.properties["jobtitle"] = json.get("jobtitle");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the person node with the modified details
|
||||||
|
person.save();
|
||||||
|
|
||||||
|
// Enable or disable account? - note that only Admin can set this
|
||||||
|
if (json.has("disableAccount"))
|
||||||
|
{
|
||||||
|
var disableAccount = (json.get("disableAccount") == true);
|
||||||
|
if (disableAccount && people.isAccountEnabled(userName))
|
||||||
|
{
|
||||||
|
people.disableAccount(userName);
|
||||||
|
}
|
||||||
|
else if (!disableAccount && !people.isAccountEnabled(userName))
|
||||||
|
{
|
||||||
|
people.enableAccount(userName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// set quota if supplied - note that only Admin can set this and will be ignored otherwise
|
||||||
|
if (json.has("quota"))
|
||||||
|
{
|
||||||
|
var quota = json.get("quota");
|
||||||
|
people.setQuota(person, quota.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove from groups if supplied - note that only Admin can do this
|
||||||
|
if (json.has("removeGroups"))
|
||||||
|
{
|
||||||
|
var groups = json.get("removeGroups");
|
||||||
|
for (var index=0; index<groups.length(); index++)
|
||||||
|
{
|
||||||
|
var groupId = groups.getString(index);
|
||||||
|
var group = people.getGroup(groupId);
|
||||||
|
if (group != null)
|
||||||
|
{
|
||||||
|
people.removeAuthority(group, person);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// app to groups if supplied - note that only Admin can do this
|
||||||
|
if (json.has("addGroups"))
|
||||||
|
{
|
||||||
|
var groups = json.get("addGroups");
|
||||||
|
for (var index=0; index<groups.length(); index++)
|
||||||
|
{
|
||||||
|
var groupId = groups.getString(index);
|
||||||
|
var group = people.getGroup(groupId);
|
||||||
|
if (group != null)
|
||||||
|
{
|
||||||
|
people.addAuthority(group, person);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put the created person into the model
|
||||||
|
model.person = person;
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
@@ -417,17 +417,6 @@
|
|||||||
<!-- Person Service REST API -->
|
<!-- Person Service REST API -->
|
||||||
<!-- -->
|
<!-- -->
|
||||||
|
|
||||||
<!-- -->
|
|
||||||
<!-- person.put Web Script - updates a person with the given person properties -->
|
|
||||||
<!-- -->
|
|
||||||
|
|
||||||
<bean id="webscript.org.alfresco.repository.person.person.put"
|
|
||||||
class="org.alfresco.repo.web.scripts.person.PersonPut"
|
|
||||||
parent="webscript">
|
|
||||||
<property name="serviceRegistry" ref="ServiceRegistry"/>
|
|
||||||
<property name="authenticationComponent" ref="AuthenticationComponent"/>
|
|
||||||
</bean>
|
|
||||||
|
|
||||||
<!-- -->
|
<!-- -->
|
||||||
<!-- changepassword.post Web Script - updates a user password -->
|
<!-- changepassword.post Web Script - updates a user password -->
|
||||||
<!-- -->
|
<!-- -->
|
||||||
|
@@ -1,213 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2005-2007 Alfresco Software Limited.
|
|
||||||
*
|
|
||||||
* This program is free software; you can redistribute it and/or
|
|
||||||
* modify it under the terms of the GNU General Public License
|
|
||||||
* as published by the Free Software Foundation; either version 2
|
|
||||||
* of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program; if not, write to the Free Software
|
|
||||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
||||||
|
|
||||||
* As a special exception to the terms and conditions of version 2.0 of
|
|
||||||
* the GPL, you may redistribute this Program in connection with Free/Libre
|
|
||||||
* and Open Source Software ("FLOSS") applications as described in Alfresco's
|
|
||||||
* FLOSS exception. You should have received a copy of the text describing
|
|
||||||
* the FLOSS exception, and it is also available here:
|
|
||||||
* http://www.alfresco.com/legal/licensing"
|
|
||||||
*/
|
|
||||||
package org.alfresco.repo.web.scripts.person;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.alfresco.model.ContentModel;
|
|
||||||
import org.alfresco.repo.security.authentication.AuthenticationComponent;
|
|
||||||
import org.alfresco.repo.security.authentication.AuthenticationUtil;
|
|
||||||
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
|
|
||||||
import org.alfresco.repo.security.permissions.AccessDeniedException;
|
|
||||||
import org.alfresco.repo.template.TemplateNode;
|
|
||||||
import org.alfresco.service.ServiceRegistry;
|
|
||||||
import org.alfresco.service.cmr.repository.NodeRef;
|
|
||||||
import org.alfresco.service.cmr.repository.NodeService;
|
|
||||||
import org.alfresco.service.cmr.security.PersonService;
|
|
||||||
import org.alfresco.service.namespace.QName;
|
|
||||||
import org.alfresco.util.Content;
|
|
||||||
import org.alfresco.web.scripts.DeclarativeWebScript;
|
|
||||||
import org.alfresco.web.scripts.Status;
|
|
||||||
import org.alfresco.web.scripts.WebScriptException;
|
|
||||||
import org.alfresco.web.scripts.WebScriptRequest;
|
|
||||||
import org.json.JSONObject;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Web Script to update a person's details
|
|
||||||
*
|
|
||||||
* @author glen dot johnson at alfresco dot com
|
|
||||||
*/
|
|
||||||
public class PersonPut extends DeclarativeWebScript
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Carries out the work of updating the given person node with the given
|
|
||||||
* person properties if the current user has the appropriate permissions to do so
|
|
||||||
*/
|
|
||||||
private class UpdatePersonPropertiesWorker implements RunAsWork<NodeRef>
|
|
||||||
{
|
|
||||||
// the person node whose properties to update
|
|
||||||
private NodeRef person;
|
|
||||||
|
|
||||||
// the properties to update the person with
|
|
||||||
private JSONObject personProps;
|
|
||||||
|
|
||||||
public UpdatePersonPropertiesWorker(NodeRef person, JSONObject personProps)
|
|
||||||
{
|
|
||||||
this.person = person;
|
|
||||||
this.personProps = personProps;
|
|
||||||
}
|
|
||||||
|
|
||||||
public NodeRef doWork() throws Exception
|
|
||||||
{
|
|
||||||
// get references to services
|
|
||||||
NodeService nodeService = PersonPut.this.serviceRegistry.getNodeService();
|
|
||||||
|
|
||||||
// Update the given person node's properties
|
|
||||||
Map<QName, Serializable> props = new HashMap<QName, Serializable>(16);
|
|
||||||
props.put(ContentModel.PROP_USERNAME, (Serializable) personProps.get("userName"));
|
|
||||||
props.put(ContentModel.PROP_TITLE, (Serializable) personProps.get("title"));
|
|
||||||
props.put(ContentModel.PROP_FIRSTNAME, (Serializable) personProps.get("firstName"));
|
|
||||||
props.put(ContentModel.PROP_LASTNAME, (Serializable) personProps.get("lastName"));
|
|
||||||
props.put(ContentModel.PROP_ORGANIZATION, (Serializable) personProps.get("organisation"));
|
|
||||||
props.put(ContentModel.PROP_JOBTITLE, (Serializable) personProps.get("jobtitle"));
|
|
||||||
props.put(ContentModel.PROP_EMAIL, (Serializable) personProps.get("email"));
|
|
||||||
try
|
|
||||||
{
|
|
||||||
nodeService.setProperties(this.person, props);
|
|
||||||
}
|
|
||||||
catch (AccessDeniedException err)
|
|
||||||
{
|
|
||||||
// catch security exception if the user does not have permissions
|
|
||||||
String currentUserName = AuthenticationUtil.getFullyAuthenticatedUser();
|
|
||||||
String personUserName = (String)nodeService.getProperty(person, ContentModel.PROP_USERNAME);
|
|
||||||
throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR, "Current user: "
|
|
||||||
+ currentUserName + " does not have the appropriate permissons to update "
|
|
||||||
+ " person node with userName: " + personUserName);
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.person;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// service and service component references
|
|
||||||
private ServiceRegistry serviceRegistry;
|
|
||||||
private AuthenticationComponent authenticationComponent;
|
|
||||||
|
|
||||||
// model property keys
|
|
||||||
private static final String MODEL_PROP_KEY_PERSON = "person";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the serviceRegistry property
|
|
||||||
*
|
|
||||||
* @param serviceRegistry the service registry to set
|
|
||||||
*/
|
|
||||||
public void setServiceRegistry(ServiceRegistry serviceRegistry)
|
|
||||||
{
|
|
||||||
this.serviceRegistry = serviceRegistry;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the authenticationComponent property
|
|
||||||
*
|
|
||||||
* @param authenticationComponent
|
|
||||||
* the authentication component to set
|
|
||||||
*/
|
|
||||||
public void setAuthenticationComponent(
|
|
||||||
AuthenticationComponent authenticationComponent)
|
|
||||||
{
|
|
||||||
this.authenticationComponent = authenticationComponent;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This method contains the logic which gets executed when the Web
|
|
||||||
* Script is run
|
|
||||||
*
|
|
||||||
* @see org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(
|
|
||||||
* org.alfresco.web.scripts.WebScriptRequest,
|
|
||||||
* org.alfresco.web.scripts.WebScriptResponse)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected Map<String, Object> executeImpl(WebScriptRequest req,
|
|
||||||
Status status)
|
|
||||||
{
|
|
||||||
// initialise model to pass on for template to render
|
|
||||||
Map<String, Object> model = new HashMap<String, Object>(1, 1.0f);
|
|
||||||
|
|
||||||
// get references to services from service registry
|
|
||||||
PersonService personService = this.serviceRegistry.getPersonService();
|
|
||||||
|
|
||||||
// get request body content
|
|
||||||
Content content = req.getContent();
|
|
||||||
if (content == null)
|
|
||||||
{
|
|
||||||
throw new WebScriptException("Failed to convert request body content to JSON object");
|
|
||||||
}
|
|
||||||
|
|
||||||
// get the person properties passed in as a JSON object
|
|
||||||
JSONObject personJsonProps = null;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
String jsonString = content.getContent();
|
|
||||||
if (jsonString.startsWith("[") == true)
|
|
||||||
{
|
|
||||||
throw new WebScriptException(
|
|
||||||
Status.STATUS_INTERNAL_SERVER_ERROR,
|
|
||||||
"Properties for multiple people appear to have been passed in through in the request "
|
|
||||||
+ "body as a JSON Array. Only one set of person properties must be passed "
|
|
||||||
+ "through. Request body content is: " + jsonString);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
personJsonProps = new JSONObject(jsonString);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception exception)
|
|
||||||
{
|
|
||||||
throw new WebScriptException("Failed to convert request body to JSON object", exception);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract user name from the URL
|
|
||||||
String userName = req.getExtensionPath();
|
|
||||||
|
|
||||||
// Get person noderef associated with that user name
|
|
||||||
NodeRef person = personService.getPerson(userName);
|
|
||||||
|
|
||||||
// if person node has been found associated with the given user name
|
|
||||||
// then update that person's details with the person properties
|
|
||||||
// provided within the JSON object (in the request body)
|
|
||||||
if (person != null)
|
|
||||||
{
|
|
||||||
RunAsWork<NodeRef> updatePersonPropertiesWorker = new UpdatePersonPropertiesWorker(
|
|
||||||
person, personJsonProps);
|
|
||||||
NodeRef updatedPerson = AuthenticationUtil.runAs(
|
|
||||||
updatePersonPropertiesWorker, this.authenticationComponent.getSystemUserName());
|
|
||||||
|
|
||||||
// Put the updated person on the model to pass to the template
|
|
||||||
model.put(MODEL_PROP_KEY_PERSON, new TemplateNode(updatedPerson, this.serviceRegistry, null));
|
|
||||||
}
|
|
||||||
// else if no person was found matching the given user name,
|
|
||||||
// then return HTTP error status "not found"
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new WebScriptException(Status.STATUS_NOT_FOUND, "Person "
|
|
||||||
+ userName + " does not exist and thus can't be updated");
|
|
||||||
}
|
|
||||||
|
|
||||||
return model;
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user