Notification if external user management is activated

A notification will be displayed when an external user
management system is activated to inform that users
created within the SCMM will not be transferred to the
external system.

Co-authored-by: René Pfeuffer<rene.pfeuffer@cloudogu.com>
This commit is contained in:
Laura Gorzitze
2024-07-04 09:05:17 +02:00
parent 1c57342cd9
commit b12f4d579a
14 changed files with 146 additions and 27 deletions

View File

@@ -0,0 +1,2 @@
- type: added
description: Notification when external user management is activated during user creation

View File

@@ -0,0 +1,32 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.user;
import sonia.scm.plugin.ExtensionPoint;
@ExtensionPoint
public interface ExternalAuthenticationAvailableNotifier {
boolean isExternalAuthenticationAvailable();
}

View File

@@ -37,6 +37,7 @@ import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import sonia.scm.NotFoundException;
import sonia.scm.repository.GitRepositoryHandler;
import sonia.scm.repository.GitWorkingCopyFactory;
import sonia.scm.repository.InternalRepositoryException;

View File

@@ -72,6 +72,7 @@ describe("Test user hooks", () => {
_embedded: {
users: [yoda],
},
externalAuthenticationAvailable: false,
};
afterEach(() => {

View File

@@ -47,7 +47,9 @@ export type UserCreation = User;
export type UserCollection = PagedCollection<{
users: User[];
}>;
}> & {
externalAuthenticationAvailable: boolean;
};
export type PermissionOverview = HalRepresentation & {
relevantGroups: PermissionOverviewGroupEntry[];

View File

@@ -50,6 +50,7 @@
"createUser": {
"title": "Benutzer erstellen",
"subtitle": "Erstellen eines neuen Benutzers",
"notification": "Ein externes Benutzerverwaltungssystem ist aktiviert. Die hier angelegten Benutzer werden nicht in die externe Benutzerverwaltung übertragen.",
"form": {
"submit": "Speichern",
"submit-success-notification": "Der Benutzer wurde erfolgreich aktualisiert",

View File

@@ -50,6 +50,7 @@
"createUser": {
"title": "Create User",
"subtitle": "Create a new user",
"notification": "An external user management system is activated. Users created here will not be transferred to the external user management.",
"form": {
"submit": "Submit",
"submit-success-notification": "The user was updated successfully",

View File

@@ -24,21 +24,21 @@
import React, { FC, useRef } from "react";
import { Redirect } from "react-router-dom";
import { useTranslation } from "react-i18next";
import { useRequiredIndexLink } from "@scm-manager/ui-api";
import { Page } from "@scm-manager/ui-components";
import { Form, useCreateResource } from "@scm-manager/ui-forms";
import * as userValidator from "../components/userValidation";
import { User, UserCreation } from "@scm-manager/ui-types";
import { Link, User, UserCollection, UserCreation } from "@scm-manager/ui-types";
import { ErrorNotification, Loading, Notification } from "@scm-manager/ui-core";
import { useUsers } from "@scm-manager/ui-api";
type UserCreationForm = Pick<UserCreation, "password" | "name" | "displayName" | "active" | "external" | "mail"> & {
passwordConfirmation: string;
};
const CreateUser: FC = () => {
const CreateUserForm: FC<{ users: UserCollection }> = ({ users }) => {
const [t] = useTranslation("users");
const indexLink = useRequiredIndexLink("users");
const { submit, submissionResult: createdUser } = useCreateResource<UserCreationForm, User>(
indexLink,
(users._links.create as Link).href,
["user", "users"],
(user) => user.name,
{
@@ -61,6 +61,9 @@ const CreateUser: FC = () => {
return (
<Page title={t("createUser.title")} subtitle={t("createUser.subtitle")} showContentOnError={true}>
{users.externalAuthenticationAvailable && (
<Notification type="warning">{t("createUser.notification")}</Notification>
)}
<Form onSubmit={submit} translationPath={["users", "createUser.form"]} defaultValues={defaultValuesRef.current}>
{({ watch }) => (
<>
@@ -114,4 +117,17 @@ const CreateUser: FC = () => {
);
};
const CreateUser: FC = () => {
const { data: users, isLoading, error } = useUsers({ page: 0 });
if (isLoading) {
return <Loading />;
}
if (error) {
return <ErrorNotification error={error} />;
}
return <CreateUserForm users={users!} />;
};
export default CreateUser;

View File

@@ -26,15 +26,8 @@ import { Redirect, useLocation, useParams } from "react-router-dom";
import { useTranslation } from "react-i18next";
import { useUsers } from "@scm-manager/ui-api";
import { User, UserCollection } from "@scm-manager/ui-types";
import {
CreateButton,
LinkPaginator,
Notification,
OverviewPageActions,
Page,
PageActions,
urls,
} from "@scm-manager/ui-components";
import { CreateButton, LinkPaginator, OverviewPageActions, Page, PageActions, urls } from "@scm-manager/ui-components";
import { Notification } from "@scm-manager/ui-core";
import { UserTable } from "./../components/table";
type UserPageProps = {

View File

@@ -55,14 +55,18 @@ abstract class PagedCollectionToDtoMapper<E extends ModelObject, D extends HalRe
CollectionDto map(int pageNumber, int pageSize, PageResult<E> pageResult, String selfLink, Optional<String> createLink, Function<E, ? extends HalRepresentation> mapper) {
NumberedPaging paging = zeroBasedNumberedPaging(pageNumber, pageSize, pageResult.getOverallCount());
List<HalRepresentation> dtos = pageResult.getEntities().stream().map(mapper).collect(toList());
CollectionDto collectionDto = new CollectionDto(
createLinks(paging, selfLink, createLink),
embedDtos(dtos));
Links links = createLinks(paging, selfLink, createLink);
Embedded embedded = embedDtos(dtos);
CollectionDto collectionDto = createCollectionDto(links, embedded);
collectionDto.setPage(pageNumber);
collectionDto.setPageTotal(computePageTotal(pageSize, pageResult));
return collectionDto;
}
CollectionDto createCollectionDto(Links links, Embedded embedded) {
return new CollectionDto(links, embedded);
}
private int computePageTotal(int pageSize, PageResult<E> pageResult) {
if (pageResult.getOverallCount() % pageSize > 0) {
return pageResult.getOverallCount() / pageSize + 1;

View File

@@ -0,0 +1,40 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.api.v2.resources;
import de.otto.edison.hal.Embedded;
import de.otto.edison.hal.Links;
import lombok.Getter;
@Getter
class UserCollectionDto extends CollectionDto {
private final boolean externalAuthenticationAvailable;
UserCollectionDto(Links links, Embedded embedded, boolean externalAuthenticationAvailable) {
super(links, embedded);
this.externalAuthenticationAvailable = externalAuthenticationAvailable;
}
}

View File

@@ -24,12 +24,16 @@
package sonia.scm.api.v2.resources;
import de.otto.edison.hal.Embedded;
import de.otto.edison.hal.Links;
import jakarta.inject.Inject;
import sonia.scm.PageResult;
import sonia.scm.user.ExternalAuthenticationAvailableNotifier;
import sonia.scm.user.User;
import sonia.scm.user.UserPermissions;
import java.util.Optional;
import java.util.Set;
import static java.util.Optional.empty;
import static java.util.Optional.of;
@@ -38,10 +42,13 @@ public class UserCollectionToDtoMapper extends BasicCollectionToDtoMapper<User,
private final ResourceLinks resourceLinks;
private final Set<ExternalAuthenticationAvailableNotifier> externalAuthenticationAvailableNotifier;
@Inject
public UserCollectionToDtoMapper(UserToUserDtoMapper userToDtoMapper, ResourceLinks resourceLinks) {
public UserCollectionToDtoMapper(UserToUserDtoMapper userToDtoMapper, ResourceLinks resourceLinks, Set<ExternalAuthenticationAvailableNotifier> externalAuthenticationAvailableNotifier) {
super("users", userToDtoMapper);
this.resourceLinks = resourceLinks;
this.externalAuthenticationAvailableNotifier = externalAuthenticationAvailableNotifier;
}
public CollectionDto map(int pageNumber, int pageSize, PageResult<User> pageResult) {
@@ -49,10 +56,27 @@ public class UserCollectionToDtoMapper extends BasicCollectionToDtoMapper<User,
}
Optional<String> createCreateLink() {
return UserPermissions.create().isPermitted() ? of(resourceLinks.userCollection().create()): empty();
return UserPermissions.create().isPermitted() ? of(resourceLinks.userCollection().create()) : empty();
}
String createSelfLink() {
return resourceLinks.userCollection().self();
}
@Override
CollectionDto createCollectionDto(Links links, Embedded embedded) {
return new UserCollectionDto(links, embedded, isExternalAuthenticationAvailable());
}
boolean isExternalAuthenticationAvailable() {
for (ExternalAuthenticationAvailableNotifier externalAuthenticationAvailable : externalAuthenticationAvailableNotifier) {
if (externalAuthenticationAvailable.isExternalAuthenticationAvailable()) {
return true;
}
}
return false;
}
}

View File

@@ -40,6 +40,7 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import static java.util.stream.Collectors.toList;
import static org.junit.Assert.*;
@@ -58,14 +59,14 @@ public class UserCollectionToDtoMapperTest {
@InjectMocks
private SubjectThreadState subjectThreadState;
@InjectMocks
private UserCollectionToDtoMapper mapper;
private URI expectedBaseUri;
private UserCollectionToDtoMapper mapper;
@Before
public void init() throws URISyntaxException {
initMocks(this);
mapper = new UserCollectionToDtoMapper(userToDtoMapper, resourceLinks, Set.of());
expectedBaseUri = baseUri.resolve(UserRootResource.USERS_PATH_V2 + "/");
subjectThreadState.bind();
ThreadContext.bind(subject);

View File

@@ -63,6 +63,7 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collection;
import java.util.Set;
import java.util.function.Predicate;
import static de.otto.edison.hal.Links.emptyLinks;
@@ -149,7 +150,7 @@ public class UserRootResourceTest {
doNothing().when(userManager).delete(userCaptor.capture());
when(userManager.getDefaultType()).thenReturn("xml");
UserCollectionToDtoMapper userCollectionToDtoMapper = new UserCollectionToDtoMapper(userToDtoMapper, resourceLinks);
UserCollectionToDtoMapper userCollectionToDtoMapper = new UserCollectionToDtoMapper(userToDtoMapper, resourceLinks, Set.of());
UserCollectionResource userCollectionResource = new UserCollectionResource(userManager, dtoToUserMapper,
userCollectionToDtoMapper, resourceLinks, passwordService);
UserPermissionResource userPermissionResource = new UserPermissionResource(permissionAssigner, permissionCollectionToDtoMapper);