mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 09:25:43 +01:00
migrate securityV1 permissions
This commit is contained in:
@@ -162,7 +162,7 @@ public class AssignedPermission implements PermissionObject, Serializable
|
|||||||
//J-
|
//J-
|
||||||
return MoreObjects.toStringHelper(this)
|
return MoreObjects.toStringHelper(this)
|
||||||
.add("name", name)
|
.add("name", name)
|
||||||
.add("groupPermisison", groupPermission)
|
.add("groupPermission", groupPermission)
|
||||||
.add("permission", permission)
|
.add("permission", permission)
|
||||||
.toString();
|
.toString();
|
||||||
//J+
|
//J+
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import javax.xml.bind.annotation.XmlRootElement;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import static java.util.Optional.ofNullable;
|
import static java.util.Optional.ofNullable;
|
||||||
@@ -46,6 +47,44 @@ public class XmlSecurityV1UpdateStep implements UpdateStep {
|
|||||||
|
|
||||||
forAllAdmins(user -> createSecurityEntry(user, false, securityStore),
|
forAllAdmins(user -> createSecurityEntry(user, false, securityStore),
|
||||||
group -> createSecurityEntry(group, true, securityStore));
|
group -> createSecurityEntry(group, true, securityStore));
|
||||||
|
|
||||||
|
mapV1Permissions(securityStore);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void mapV1Permissions(ConfigurationEntryStore<AssignedPermission> securityStore) throws JAXBException {
|
||||||
|
Path v1SecurityFile = determineConfigDirectory().resolve("securityV1" + StoreConstants.FILE_EXTENSION);
|
||||||
|
|
||||||
|
if (!v1SecurityFile.toFile().exists()) {
|
||||||
|
LOG.info("no v1 file for security found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
JAXBContext jaxbContext = JAXBContext.newInstance(XmlSecurityV1UpdateStep.V1Security.class);
|
||||||
|
V1Security v1Security = (V1Security) jaxbContext.createUnmarshaller().unmarshal(v1SecurityFile.toFile());
|
||||||
|
|
||||||
|
v1Security.entries.forEach(assignedPermission -> {
|
||||||
|
|
||||||
|
String newPermission = "";
|
||||||
|
if (assignedPermission.value.permission != null && !assignedPermission.value.permission.isEmpty()) {
|
||||||
|
String[] splitPermission = assignedPermission.value.permission.split(":");
|
||||||
|
switch(splitPermission[2]) {
|
||||||
|
case "OWNER":
|
||||||
|
newPermission = "repository:*";
|
||||||
|
break;
|
||||||
|
case "WRITE":
|
||||||
|
newPermission = "repository:read,pull,push:*";
|
||||||
|
break;
|
||||||
|
case "READ":
|
||||||
|
newPermission = "repository:read,pull:*";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
securityStore.put(new AssignedPermission(
|
||||||
|
assignedPermission.value.name,
|
||||||
|
Boolean.parseBoolean(assignedPermission.value.groupPermission),
|
||||||
|
newPermission
|
||||||
|
));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void forAllAdmins(Consumer<String> userConsumer, Consumer<String> groupConsumer) throws JAXBException {
|
private void forAllAdmins(Consumer<String> userConsumer, Consumer<String> groupConsumer) throws JAXBException {
|
||||||
@@ -70,10 +109,9 @@ public class XmlSecurityV1UpdateStep implements UpdateStep {
|
|||||||
Arrays.stream(entries.split(",")).forEach(consumer);
|
Arrays.stream(entries.split(",")).forEach(consumer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Version getTargetVersion() {
|
public Version getTargetVersion() {
|
||||||
return parse("2.0.0");
|
return parse("2.0.1");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -102,4 +140,29 @@ public class XmlSecurityV1UpdateStep implements UpdateStep {
|
|||||||
@XmlElement(name = "admin-groups")
|
@XmlElement(name = "admin-groups")
|
||||||
private String adminGroups;
|
private String adminGroups;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@XmlAccessorType(XmlAccessType.FIELD)
|
||||||
|
@XmlRootElement(name = "configuration")
|
||||||
|
private static class V1Security {
|
||||||
|
@XmlElement(name = "entry")
|
||||||
|
private List<Entry> entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlAccessorType(XmlAccessType.FIELD)
|
||||||
|
private static class Entry {
|
||||||
|
@XmlElement(name = "key")
|
||||||
|
private String key;
|
||||||
|
@XmlElement(name = "value")
|
||||||
|
private Value value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlAccessorType(XmlAccessType.FIELD)
|
||||||
|
private static class Value {
|
||||||
|
@XmlElement(name = "permission")
|
||||||
|
String permission;
|
||||||
|
@XmlElement(name = "name")
|
||||||
|
String name;
|
||||||
|
@XmlElement(name = "group-permission")
|
||||||
|
String groupPermission;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,7 +57,8 @@ public class XmlUserV1UpdateStep implements UpdateStep {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doUpdate() throws JAXBException {
|
public void doUpdate() throws JAXBException {
|
||||||
Optional<Path> v1UsersFile = determineV1File();
|
Optional<Path> v1UsersFile = determineV1File("users");
|
||||||
|
determineV1File("security");
|
||||||
if (!v1UsersFile.isPresent()) {
|
if (!v1UsersFile.isPresent()) {
|
||||||
LOG.info("no v1 file for users found");
|
LOG.info("no v1 file for users found");
|
||||||
return;
|
return;
|
||||||
@@ -107,17 +108,17 @@ public class XmlUserV1UpdateStep implements UpdateStep {
|
|||||||
return configurationEntryStoreFactory.withType(AssignedPermission.class).withName("security").build();
|
return configurationEntryStoreFactory.withType(AssignedPermission.class).withName("security").build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<Path> determineV1File() {
|
private Optional<Path> determineV1File(String filename) {
|
||||||
Path existingUsersFile = resolveConfigFile("users");
|
Path existingFile = resolveConfigFile(filename);
|
||||||
Path usersV1File = resolveConfigFile("usersV1");
|
Path v1File = resolveConfigFile(filename + "V1");
|
||||||
if (existingUsersFile.toFile().exists()) {
|
if (existingFile.toFile().exists()) {
|
||||||
try {
|
try {
|
||||||
Files.move(existingUsersFile, usersV1File);
|
Files.move(existingFile, v1File);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new UpdateException("could not move old users file to " + usersV1File.toAbsolutePath());
|
throw new UpdateException("could not move old " + filename + " file to " + v1File.toAbsolutePath());
|
||||||
}
|
}
|
||||||
LOG.info("moved old users file to {}", usersV1File.toAbsolutePath());
|
LOG.info("moved old " + filename + " file to {}", v1File.toAbsolutePath());
|
||||||
return of(usersV1File);
|
return of(v1File);
|
||||||
}
|
}
|
||||||
return empty();
|
return empty();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user