Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ldapadmin - administrators can modify users' uid #1109

Merged
merged 4 commits into from Nov 3, 2015
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -58,6 +58,19 @@ public interface AccountDao {
*/
void update(final Account account) throws DataServiceException, DuplicatedEmailException;

/**
* Updates the user account, given the old and the new state of the account
* Needed if a DN update is required (modifying the uid).
*
* @param account
* @param modified
*
* @throws DuplicatedEmailException
* @throws DataServiceException
* @throws NotFoundException
*/
void update(Account account, Account modified) throws DataServiceException, DuplicatedEmailException, NotFoundException;

/**
* Changes the user password
*
Expand Down Expand Up @@ -123,8 +136,4 @@ public interface AccountDao {
*/
String generateUid(String uid) throws DataServiceException;





}
Expand Up @@ -16,6 +16,7 @@
import org.apache.commons.logging.LogFactory;
import org.georchestra.ldapadmin.dto.Account;
import org.georchestra.ldapadmin.dto.AccountFactory;
import org.georchestra.ldapadmin.dto.Group;
import org.georchestra.ldapadmin.dto.UserSchema;
import org.georchestra.ldapadmin.ws.newaccount.UidGenerator;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -190,7 +191,7 @@ public Object mapFromAttributes(Attributes attributes) throws NamingException {
* @see {@link AccountDao#update(Account)}
*/
@Override
public void update(final Account account) throws DataServiceException, DuplicatedEmailException {
public synchronized void update(final Account account) throws DataServiceException, DuplicatedEmailException {

// checks mandatory fields
if (account.getUid().length() == 0) {
Expand Down Expand Up @@ -233,13 +234,27 @@ public void update(final Account account) throws DataServiceException, Duplicate
ldapTemplate.modifyAttributes(context);
}

/**
* @see {@link AccountDao#update(Account, Account)}
*/
@Override
public synchronized void update(Account account, Account modified) throws DataServiceException, DuplicatedEmailException, NotFoundException {
if (! account.getUid().equals(modified.getUid())) {
ldapTemplate.rename(buildDn(account.getUid()), buildDn(modified.getUid()));
for (Group g : groupDao.findAllForUser(account.getUid())) {
groupDao.modifyUser(g.getName(), account.getUid(), modified.getUid());
}
}
update(modified);
}

/**
* Removes the user account and the reference included in the group
*
* @see {@link AccountDao#delete(Account)}
*/
@Override
public void delete(final String uid) throws DataServiceException, NotFoundException {
public synchronized void delete(final String uid) throws DataServiceException, NotFoundException {
this.ldapTemplate.unbind(buildDn(uid), true);

this.groupDao.deleteUser(uid);
Expand Down Expand Up @@ -572,5 +587,4 @@ public String generateUid(String uid) throws DataServiceException {

return newUid;
}

}
28 changes: 22 additions & 6 deletions ldapadmin/src/main/java/org/georchestra/ldapadmin/ds/GroupDao.java
Expand Up @@ -30,34 +30,50 @@ public interface GroupDao {
* @return list of {@link Group}
*/
List<Group> findAll() throws DataServiceException;


/**
* Returns all groups for a given uid.
*
* @return list of {@link Group}
*/
List<Group> findAllForUser(String userId) throws DataServiceException;

/**
* Returns the group's users
*
* @return list of user uid
*/
List<String> findUsers(final String groupName) throws DataServiceException;


/**
* Deletes the user from all groups
*
*
* @param uid
* @throws DataServiceException
*/
void deleteUser(String uid) throws DataServiceException;

void deleteUsers(String cn, List<String> deleteList) throws DataServiceException, NotFoundException;

/**
* Deletes the user from the user
* Deletes the user from the group
*
* @param groupName
* @param uid
* @throws DataServiceException
*/
void deleteUser(String groupName, String uid) throws DataServiceException;

/**
* Modifies the user (e.g. rename) from the group
*
* @param groupName
* @param oldUid
* @param newUid
* @throws DataServiceException
*/
void modifyUser(String groupName, String oldUid, String newUid) throws DataServiceException;

/**
* Adds the group
*
Expand Down
Expand Up @@ -11,6 +11,7 @@

import javax.naming.InvalidNameException;
import javax.naming.Name;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
Expand Down Expand Up @@ -158,6 +159,17 @@ public void deleteUser(String groupName, String uid) throws DataServiceException
this.ldapTemplate.modifyAttributes(ctx);
}

@Override
public void modifyUser(String groupName, String oldUid, String newUid) throws DataServiceException {
Name dnGroup = buildGroupDn(groupName);
String oldUserDn = buildUserDn(oldUid).toString();
String newUserDn = buildUserDn(newUid).toString();
DirContextOperations ctx = ldapTemplate.lookupContext(dnGroup);
ctx.removeAttributeValue("member", oldUserDn);
ctx.addAttributeValue("member", newUserDn);
this.ldapTemplate.modifyAttributes(ctx);
}

public List<Group> findAll() throws DataServiceException {

EqualsFilter filter = new EqualsFilter("objectClass", "groupOfMembers");
Expand All @@ -172,6 +184,16 @@ public List<Group> findAll() throws DataServiceException {
return new LinkedList<Group>(sorted);
}

public List<Group> findAllForUser(String userId) {
EqualsFilter grpFilter = new EqualsFilter("objectClass", "groupOfMembers");
AndFilter filter = new AndFilter();
filter.and(grpFilter);

filter.and(new EqualsFilter("member", buildUserDn(userId).toString()));
return ldapTemplate.search(DistinguishedName.EMPTY_PATH, filter.encode(),
new GroupContextMapper());
}

public List<String> findUsers(final String groupName) throws DataServiceException{

AndFilter filter = new AndFilter();
Expand Down Expand Up @@ -458,5 +480,4 @@ public void deleteUsersInGroups(List<String> deleteGroup, List<String> users)

}


}
Expand Up @@ -184,4 +184,38 @@ public static Account createFull(
return a;
}

/**
* Creates an account object from another one, given as argument.
*
* @param o other account to copy
*/
public static Account create(Account o) {
Account a = new AccountImpl();
a.setUid(o.getUid());
a.setCommonName(o.getCommonName());
a.setSurname(o.getSurname());
a.setOrg(o.getOrg());
a.setEmail(o.getEmail());
a.setPhone(o.getPhone());
a.setDescription(o.getDescription());
// passwords / new passwords fields voluntarily omitted:
// the password update process should not go through this.
a.setGivenName(o.getGivenName());
a.setTitle(o.getTitle());
a.setPostalAddress(o.getPostalAddress());
a.setPostalCode(o.getPostalCode());
a.setRegisteredAddress(o.getRegisteredAddress());
a.setPostOfficeBox(o.getPostOfficeBox());
a.setPhysicalDeliveryOfficeName(o.getPhysicalDeliveryOfficeName());
a.setStreet(o.getStreet());
a.setLocality(o.getLocality());
a.setFacsimile(o.getFacsimile());
a.setMobile(o.getMobile());
a.setRoomNumber(o.getRoomNumber());
a.setStateOrProvince(o.getStateOrProvince());
a.setOrganizationalUnit(o.getOrganizationalUnit());
a.setHomePostalAddress(o.getHomePostalAddress());
return a;
}

}
Expand Up @@ -11,6 +11,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.georchestra.ldapadmin.ds.AccountDao;
Expand All @@ -21,6 +22,7 @@
import org.georchestra.ldapadmin.ds.ProtectedUserFilter;
import org.georchestra.ldapadmin.dto.Account;
import org.georchestra.ldapadmin.dto.AccountFactory;
import org.georchestra.ldapadmin.dto.AccountImpl;
import org.georchestra.ldapadmin.dto.Group;
import org.georchestra.ldapadmin.dto.UserSchema;
import org.georchestra.ldapadmin.ws.backoffice.utils.RequestUtil;
Expand Down Expand Up @@ -379,9 +381,8 @@ public void update( HttpServletRequest request, HttpServletResponse response) th

// modifies the account data
try{
final Account modified = modifyAccount( account, request.getInputStream());

this.accountDao.update(modified);
final Account modified = modifyAccount(AccountFactory.create(account), request.getInputStream());
this.accountDao.update(account, modified);

ResponseUtil.writeSuccess(response);

Expand All @@ -390,13 +391,16 @@ public void update( HttpServletRequest request, HttpServletResponse response) th

ResponseUtil.buildResponse(response, jsonResponse, HttpServletResponse.SC_CONFLICT);
} catch (IOException e) {
String jsonResponse = ResponseUtil.buildResponseMessage(Boolean.FALSE, PARAMS_NOT_UNDERSTOOD);
ResponseUtil.buildResponse(response, jsonResponse, HttpServletResponse.SC_BAD_REQUEST);
throw e;
String jsonResponse = ResponseUtil.buildResponseMessage(Boolean.FALSE, PARAMS_NOT_UNDERSTOOD);
ResponseUtil.buildResponse(response, jsonResponse, HttpServletResponse.SC_BAD_REQUEST);
throw e;
} catch (DataServiceException e){
LOG.error(e.getMessage());
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
throw new IOException(e);
} catch (NotFoundException e) {
LOG.error(e.getMessage());
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
}

Expand Down Expand Up @@ -529,17 +533,19 @@ private Account modifyAccount(Account account, ServletInputStream inputStream) t
account.setDescription(description);
}



String commonName = AccountFactory.formatCommonName(
account.getGivenName(), account.getSurname());

account.setCommonName(commonName);

String uid = RequestUtil.getFieldValue(json, UserSchema.UUID_KEY);
if (uid != null) {
account.setUid(uid);
}
return account;

}



/**
* Create a new account from the body request.
*
Expand Down
19 changes: 18 additions & 1 deletion ldapadmin/src/main/webapp/privateui/js/controllers.js
Expand Up @@ -259,11 +259,28 @@ angular.module('ldapadmin.controllers', [])
$scope.save = function() {
$scope.user.put().then(function() {
flash.success = 'User correctly updated';
var index = findByAttr($scope.users, 'uid', $routeParams.userId);
var prevUserId = $routeParams.userId;
var newUserId = $scope.user.uid;
var index = findByAttr($scope.users, 'uid', prevUserId);

if (index !== false) {
$scope.users[index] = angular.copy($scope.user);
remote = angular.copy($scope.user);

// uid modified
if (newUserId != prevUserId) {
window.location = '#/users/' + newUserId;

// Update the groups the user belongs to
var i,
len = $scope.groups.length;
for (i=0; i < len; i++) {
var index2 = _.indexOf($scope.groups[i].users, prevUserId);
if (index2 != -1) {
$scope.groups[i].users[index2] = newUserId;
}
}
}
}
}, function(args) {
flash.error = 'User could not be updated';
Expand Down
Expand Up @@ -25,7 +25,7 @@
<div class="span6">
<div class="control-group">
<label for="mail">Login</label>
<input type="text" name="uid" ng-model="user.uid" ng-readonly="true" placeholder="Login" class="span12"/>
<input type="text" name="uid" ng-model="user.uid" placeholder="Login" class="span12"/>
</div>
</div>
<div class="span6">
Expand Down