use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.
the class GitContentRepository method moveContent.
@Override
public Map<String, String> moveContent(String site, String fromPath, String toPath, String newName) {
Map<String, String> toRet = new TreeMap<String, String>();
String commitId;
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
generalLockService.lock(gitLockKey);
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
synchronized (helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX)) {
Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
String gitFromPath = helper.getGitPath(fromPath);
String gitToPath;
if (StringUtils.isEmpty(newName)) {
gitToPath = helper.getGitPath(toPath);
} else {
gitToPath = helper.getGitPath(toPath + FILE_SEPARATOR + newName);
}
try (Git git = new Git(repo)) {
// Check if destination is a file, then this is a rename operation
// Perform rename and exit
Path sourcePath = Paths.get(repo.getDirectory().getParent(), gitFromPath);
File sourceFile = sourcePath.toFile();
Path targetPath = Paths.get(repo.getDirectory().getParent(), gitToPath);
File targetFile = targetPath.toFile();
if (sourceFile.getCanonicalFile().equals(targetFile.getCanonicalFile())) {
sourceFile.renameTo(targetFile);
} else {
if (targetFile.isFile()) {
if (sourceFile.isFile()) {
sourceFile.renameTo(targetFile);
} else {
// This is not a valid operation
logger.error("Invalid move operation: Trying to rename a directory to a file " + "for site: " + site + " fromPath: " + fromPath + " toPath: " + toPath + " newName: " + newName);
}
} else if (sourceFile.isDirectory()) {
// Check if we're moving a single file or whole subtree
File[] dirList = sourceFile.listFiles();
for (File child : dirList) {
if (!child.equals(sourceFile)) {
FileUtils.moveToDirectory(child, targetFile, true);
}
}
FileUtils.deleteDirectory(sourceFile);
} else {
if (sourceFile.isFile()) {
FileUtils.moveFile(sourceFile, targetFile);
} else {
FileUtils.moveToDirectory(sourceFile, targetFile, true);
}
}
}
// The operation is done on disk, now it's time to commit
AddCommand addCommand = git.add().addFilepattern(gitToPath);
retryingRepositoryOperationFacade.call(addCommand);
StatusCommand statusCommand = git.status().addPath(gitToPath);
Status gitStatus = retryingRepositoryOperationFacade.call(statusCommand);
Set<String> changeSet = gitStatus.getAdded();
for (String pathToCommit : changeSet) {
String pathRemoved = pathToCommit.replace(gitToPath, gitFromPath);
CommitCommand commitCommand = git.commit().setOnly(pathToCommit).setOnly(pathRemoved).setAuthor(helper.getCurrentUserIdent()).setCommitter(helper.getCurrentUserIdent()).setMessage(helper.getCommitMessage(REPO_MOVE_CONTENT_COMMIT_MESSAGE).replaceAll(PATTERN_FROM_PATH, fromPath).replaceAll(PATTERN_TO_PATH, toPath + (StringUtils.isNotEmpty(newName) ? newName : EMPTY)));
RevCommit commit = retryingRepositoryOperationFacade.call(commitCommand);
commitId = commit.getName();
toRet.put(pathToCommit, commitId);
}
}
}
} catch (IOException | GitAPIException | ServiceLayerException | UserNotFoundException | CryptoException e) {
logger.error("Error while moving content for site: " + site + " fromPath: " + fromPath + " toPath: " + toPath + " newName: " + newName);
} finally {
generalLockService.unlock(gitLockKey);
}
return toRet;
}
use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.
the class SecurityServiceImpl method validateToken.
@Override
@ValidateParams
public boolean validateToken(@ValidateStringParam(name = "token") String token) throws UserNotFoundException, UserExternallyManagedException, ServiceLayerException {
boolean toRet = false;
String decryptedToken = decryptToken(token);
if (StringUtils.isNotEmpty(decryptedToken)) {
StringTokenizer tokenElements = new StringTokenizer(decryptedToken, "|");
if (tokenElements.countTokens() == 3) {
String username = tokenElements.nextToken();
User userProfile = userServiceInternal.getUserByIdOrUsername(-1, username);
if (userProfile == null) {
logger.info("User profile not found for " + username);
throw new UserNotFoundException();
} else {
if (userProfile.isExternallyManaged()) {
throw new UserExternallyManagedException();
} else {
long tokenTimestamp = Long.parseLong(tokenElements.nextToken());
if (tokenTimestamp < System.currentTimeMillis()) {
toRet = false;
} else {
toRet = true;
}
}
}
}
}
return toRet;
}
use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.
the class SecurityServiceImpl method isSiteAdmin.
@Override
@ValidateParams
public boolean isSiteAdmin(@ValidateStringParam(name = "username") String username, String site) {
boolean toRet = false;
try {
if (userServiceInternal.isUserMemberOfGroup(username, SYSTEM_ADMIN_GROUP)) {
return true;
}
List<Group> groups = userServiceInternal.getUserGroups(-1, username);
if (CollectionUtils.isNotEmpty(groups)) {
Map<String, List<String>> roleMappings = configurationService.geRoleMappings(site);
if (MapUtils.isNotEmpty(roleMappings)) {
for (Group group : groups) {
String groupName = group.getGroupName();
List<String> roles = roleMappings.get(groupName);
if (roles.contains(ADMIN_ROLE)) {
toRet = true;
}
}
}
}
} catch (ServiceLayerException | UserNotFoundException e) {
logger.warn("Error getting user memberships", e);
}
return toRet;
}
use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.
the class SecurityServiceImpl method getUserRoles.
@Override
@ValidateParams
public Set<String> getUserRoles(@ValidateStringParam(name = "site") final String site, @ValidateStringParam(name = "user") String user, boolean includeGlobal) {
try {
// TODO: We should replace this with userService.getUserSiteRoles, but that one is protected by permissions.
// TODO: When the UserService is refactored to use UserServiceInternal, we could use that method and
// TODO: remove this one
List<Group> groups = userServiceInternal.getUserGroups(-1, user);
if (groups != null && groups.size() > 0) {
logger.debug("Groups for " + user + " in " + site + ": " + groups);
PermissionsConfigTO rolesConfig = loadConfiguration(site, getRoleMappingsFileName());
Set<String> userRoles = new HashSet<String>();
if (rolesConfig != null) {
Map<String, List<String>> rolesMap = rolesConfig.getRoles();
for (Group group : groups) {
String groupName = group.getGroupName();
if (StringUtils.equals(groupName, SYSTEM_ADMIN_GROUP)) {
Collection<List<String>> mapValues = rolesMap.values();
mapValues.forEach(valueList -> {
userRoles.addAll(valueList);
});
break;
} else {
List<String> roles = rolesMap.get(groupName);
if (roles != null) {
userRoles.addAll(roles);
}
}
}
}
if (includeGlobal) {
PermissionsConfigTO globalRolesConfig = loadGlobalRolesConfiguration();
addGlobalUserRoles(user, userRoles, globalRolesConfig);
List<String> groupNames = groups.stream().map(x -> x.getGroupName()).collect(Collectors.toList());
addGlobalGroupRoles(userRoles, groupNames, globalRolesConfig);
}
return userRoles;
} else {
logger.debug("No groups found for " + user + " in " + site);
}
} catch (ServiceLayerException | UserNotFoundException e) {
logger.error("Error while getting groups for user {0}", e);
}
return new HashSet<>(0);
}
use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.
the class SecurityServiceImpl method getUserProfileByGitName.
@Override
public Map<String, Object> getUserProfileByGitName(@ValidateStringParam(name = "firstNameLastName") String gitName) throws ServiceLayerException, UserNotFoundException {
Map<String, Object> toRet = new HashMap<String, Object>();
User u = userServiceInternal.getUserByGitName(gitName);
if (u != null) {
toRet.put(KEY_USERNAME, u.getUsername());
toRet.put(KEY_FIRSTNAME, u.getFirstName());
toRet.put(KEY_LASTNAME, u.getLastName());
toRet.put(KEY_EMAIL, u.getEmail());
toRet.put(KEY_EXTERNALLY_MANAGED, u.isExternallyManaged());
String authenticationType = studioConfiguration.getProperty(SECURITY_TYPE);
toRet.put(SECURITY_AUTHENTICATION_TYPE, authenticationType);
} else {
throw new UserNotFoundException("User " + gitName + " not found");
}
return toRet;
}
Aggregations