Skip to content

Commit

Permalink
Allow setting users together with directories, improve users endpoint
Browse files Browse the repository at this point in the history
Users (and groups) directly belong to a directory. Because of that, the
cleanest way to declaratively add or update directory users (and groups)
would be to set them as a child element of a directory. E.g.:

```
directories:
  internal:
    name: Internal Directory
    ...
    users:
      user1:
        password: ...
      user2:
        password: ...
```

With this change it is possible to set users together with directories,
however for groups this still needs to be implemented if needed.

Also improve the users endpoint itself and allow passing a directory ID
when performing user actions. The old behaviour that didn't require a
directory ID is inacurate because would just any random directory that
contains a user with the given name, and thus the related methods have
been deprecated.
  • Loading branch information
pathob committed Nov 8, 2023
1 parent 272f667 commit b7db617
Show file tree
Hide file tree
Showing 10 changed files with 782 additions and 57 deletions.
73 changes: 73 additions & 0 deletions index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3402,6 +3402,18 @@ endif::internal-generation[]
|
|

| groups
|
| List of <<GroupBean>>
|
|

| users
|
| List of <<UserBean>>
|
|

| schema
|
| DirectoryLdapSchema
Expand Down Expand Up @@ -4205,6 +4217,18 @@ endif::internal-generation[]
|
|

| groups
|
| List of <<GroupBean>>
|
|

| users
|
| List of <<UserBean>>
|
|

|===


Expand Down Expand Up @@ -4515,6 +4539,37 @@ endif::internal-generation[]
|===


[#GroupBean]
=== _GroupBean_



[.fields-GroupBean]
[cols="2,1,2,4,1"]
|===
| Field Name| Required| Type| Description| Format

| name
|
| String
|
|

| description
|
| String
|
|

| active
|
| Boolean
|
|

|===


[#LicenseBean]
=== _LicenseBean_

Expand Down Expand Up @@ -4813,6 +4868,18 @@ endif::internal-generation[]
|
|

| firstName
|
| String
|
|

| lastName
|
| String
|
|

| fullName
|
| String
Expand All @@ -4831,6 +4898,12 @@ endif::internal-generation[]
|
|

| groups
|
| List of <<GroupBean>>
|
|

|===


4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>

<artifactId>confapi-crowd-plugin</artifactId>
<version>0.2.2-SNAPSHOT</version>
<version>0.3.0-SNAPSHOT</version>
<packaging>atlassian-plugin</packaging>

<name>ConfAPI for Crowd</name>
Expand Down Expand Up @@ -66,7 +66,7 @@
<amps.version>8.0.2</amps.version>
<atlassian.plugin.key>${project.groupId}.${project.artifactId}</atlassian.plugin.key>
<atlassian.spring.scanner.version>2.1.5</atlassian.spring.scanner.version>
<confapi-commons.version>0.1.1</confapi-commons.version>
<confapi-commons.version>0.2.0-SNAPSHOT</confapi-commons.version>
<plugin.testrunner.version>2.0.1</plugin.testrunner.version>
<!-- Compiler must be 8 so that the plugin can run on Crowd instances using Java 8 -->
<maven.compiler.source>8</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package de.aservo.confapi.crowd.exception;

import de.aservo.confapi.commons.exception.NotFoundException;
import de.aservo.confapi.commons.model.AbstractDirectoryBean;

@SuppressWarnings("java:S110")
public class NotFoundExceptionForDirectory extends NotFoundException {

public NotFoundExceptionForDirectory(
final AbstractDirectoryBean directoryBean) {

this(directoryBean.getName());
}

public NotFoundExceptionForDirectory(
final String name) {

super(String.format("Directory with name '%s' could not be found", name));
}

public NotFoundExceptionForDirectory(
final long id) {

super(String.format("Directory with id '%s' could not be found", id));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package de.aservo.confapi.crowd.exception;

import de.aservo.confapi.commons.exception.NotFoundException;
import de.aservo.confapi.commons.model.UserBean;

@SuppressWarnings("java:S110")
public class NotFoundExceptionForUser extends NotFoundException {

public NotFoundExceptionForUser(
final UserBean userBean) {

this(userBean.getUsername());
}

public NotFoundExceptionForUser(
final String name) {

super(String.format("User with name '%s' could not be found", name));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.atlassian.crowd.model.user.User;
import de.aservo.confapi.commons.model.UserBean;

import javax.annotation.Nullable;
import javax.annotation.Nonnull;

public class UserBeanUtil {

Expand All @@ -13,16 +13,14 @@ public class UserBeanUtil {
* @param user the user
* @return the user bean
*/
@Nullable
@Nonnull
public static UserBean toUserBean(
@Nullable final User user) {

if (user == null) {
return null;
}
@Nonnull final User user) {

final UserBean userBean = new UserBean();
userBean.setUsername(user.getName());
userBean.setFirstName(user.getFirstName());
userBean.setLastName(user.getLastName());
userBean.setFullName(user.getDisplayName());
userBean.setEmail(user.getEmailAddress());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import de.aservo.confapi.commons.exception.BadRequestException;
import de.aservo.confapi.commons.exception.InternalServerErrorException;
import de.aservo.confapi.commons.exception.NotFoundException;
import de.aservo.confapi.commons.exception.ServiceUnavailableException;
import de.aservo.confapi.commons.model.AbstractDirectoryBean;
import de.aservo.confapi.commons.model.DirectoriesBean;
import de.aservo.confapi.commons.model.DirectoryInternalBean;
import de.aservo.confapi.commons.service.api.DirectoriesService;
import de.aservo.confapi.commons.service.api.UsersService;
import de.aservo.confapi.crowd.exception.NotFoundExceptionForDirectory;
import de.aservo.confapi.crowd.model.util.DirectoryBeanUtil;
import org.springframework.stereotype.Component;

Expand All @@ -40,11 +42,16 @@ public class DirectoriesServiceImpl implements DirectoriesService {
@ComponentImport
private final DirectoryManager directoryManager;

@ComponentImport
private final UsersService usersService;

@Inject
public DirectoriesServiceImpl(
final DirectoryManager directoryManager) {
final DirectoryManager directoryManager,
final UsersService usersService) {

this.directoryManager = directoryManager;
this.usersService = usersService;
}

@Override
Expand Down Expand Up @@ -89,11 +96,12 @@ public AbstractDirectoryBean setDirectory(
final boolean testConnection) {

final Directory existingDirectory = findDirectory(id);
final AbstractDirectoryBean resultDirectoryBean;

try {
final Directory mergedDirectory = DirectoryBeanUtil.toDirectory(directoryBean, existingDirectory);
final Directory updatedDirectory = directoryManager.updateDirectory(mergedDirectory);
return DirectoryBeanUtil.toDirectoryInternalBean(updatedDirectory);
resultDirectoryBean = DirectoryBeanUtil.toDirectoryBean(updatedDirectory);
} catch (DirectoryBeanUtil.UnsupportedDirectoryBeanException e) {
throw new BadRequestException(String.format(
"Setting directory type '%s' is not supported (yet)", e.getMessage()));
Expand All @@ -102,23 +110,43 @@ public AbstractDirectoryBean setDirectory(
throw new InternalServerErrorException(String.format(
"When trying to update directory '%s', it could not be found anymore", existingDirectory.getName()));
}

if (DirectoryInternalBean.class.equals(directoryBean.getClass()) && directoryBean.getClass().equals(resultDirectoryBean.getClass())) {
final DirectoryInternalBean directoryInternalBean = (DirectoryInternalBean) directoryBean;
final DirectoryInternalBean resultDirectoryInternalBean = (DirectoryInternalBean) resultDirectoryBean;

resultDirectoryInternalBean.setUsers(usersService.setUsers(directoryInternalBean.getId(), directoryInternalBean.getUsers()));
}

return resultDirectoryBean;
}

@Override
public AbstractDirectoryBean addDirectory(
final @NotNull AbstractDirectoryBean directoryBean,
final boolean testConnection) {

final AbstractDirectoryBean resultDirectoryBean;

try {
final Directory directory = DirectoryBeanUtil.toDirectory(directoryBean);
final Directory addedDirectory = directoryManager.addDirectory(directory);
return DirectoryBeanUtil.toDirectoryBean(addedDirectory);
resultDirectoryBean = DirectoryBeanUtil.toDirectoryBean(addedDirectory);
} catch (DirectoryBeanUtil.UnsupportedDirectoryBeanException e) {
throw new BadRequestException(String.format(
"Adding directory type '%s' is not supported (yet)", e.getMessage()));
} catch (DirectoryInstantiationException e) {
throw new InternalServerErrorException(String.format("Could not create directory '%s'", directoryBean.getName()));
}

if (DirectoryInternalBean.class.equals(directoryBean.getClass()) && directoryBean.getClass().equals(resultDirectoryBean.getClass())) {
final DirectoryInternalBean directoryInternalBean = (DirectoryInternalBean) directoryBean;
final DirectoryInternalBean resultDirectoryInternalBean = (DirectoryInternalBean) resultDirectoryBean;

resultDirectoryInternalBean.setUsers(usersService.setUsers(directoryInternalBean.getId(), directoryInternalBean.getUsers()));
}

return resultDirectoryBean;
}

@Override
Expand Down Expand Up @@ -164,7 +192,7 @@ Directory findDirectory(
try {
return directoryManager.findDirectoryById(id);
} catch (DirectoryNotFoundException e) {
throw new NotFoundException(e);
throw new NotFoundExceptionForDirectory(id);
}
}

Expand Down
Loading

0 comments on commit b7db617

Please sign in to comment.