use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.
the class StudioSiteAPIAccessDecisionVoter method vote.
@Override
public int vote(Authentication authentication, Object o, Collection collection) {
int toRet = ACCESS_ABSTAIN;
String requestUri = "";
if (o instanceof FilterInvocation) {
FilterInvocation filterInvocation = (FilterInvocation) o;
HttpServletRequest request = filterInvocation.getRequest();
requestUri = request.getRequestURI().replace(request.getContextPath(), "");
String userParam = request.getParameter("username");
if (StringUtils.isEmpty(userParam) && StringUtils.equalsIgnoreCase(request.getMethod(), HttpMethod.POST.name()) && !ServletFileUpload.isMultipartContent(request)) {
try {
InputStream is = request.getInputStream();
is.mark(0);
String jsonString = IOUtils.toString(is);
if (StringUtils.isNoneEmpty(jsonString)) {
JSONObject jsonObject = JSONObject.fromObject(jsonString);
if (jsonObject.has("username")) {
userParam = jsonObject.getString("username");
}
}
is.reset();
} catch (IOException | JSONException e) {
// TODO: ??
logger.debug("Failed to extract username from POST request");
}
}
User currentUser = null;
try {
String username = authentication.getPrincipal().toString();
currentUser = userServiceInternal.getUserByIdOrUsername(-1, username);
} catch (ClassCastException | UserNotFoundException | ServiceLayerException e) {
// anonymous user
if (!authentication.getPrincipal().toString().equals("anonymousUser")) {
logger.info("Error getting current user", e);
return ACCESS_ABSTAIN;
}
}
switch(requestUri) {
case CREATE:
case DELETE:
if (currentUser != null && isAdmin(currentUser)) {
toRet = ACCESS_GRANTED;
} else {
toRet = ACCESS_DENIED;
}
break;
default:
toRet = ACCESS_ABSTAIN;
break;
}
}
logger.debug("Request: " + requestUri + " - Access: " + toRet);
return toRet;
}
use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.
the class StudioAbstractAccessDecisionVoter method isSiteMember.
protected boolean isSiteMember(String siteId, User currentUser) {
try {
int total = siteService.getSitesPerUserTotal(currentUser.getUsername());
List<SiteFeed> sitesFeed = siteService.getSitesPerUser(currentUser.getUsername(), 0, total);
Set<String> sites = new HashSet<String>();
for (SiteFeed site : sitesFeed) {
sites.add(site.getSiteId());
}
return sites.contains(siteId);
} catch (UserNotFoundException e) {
logger.info("User is not site member", e);
return false;
} catch (ServiceLayerException e) {
logger.warn("Error getting user membership", e);
return false;
}
}
use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.
the class StudioAbstractAccessDecisionVoter method isAdmin.
protected boolean isAdmin(User user) {
List<Group> userGroups = null;
try {
userGroups = userServiceInternal.getUserGroups(-1, user.getUsername());
} catch (ServiceLayerException | UserNotFoundException e) {
logger.error("Error getting user memberships", e);
return false;
}
boolean toRet = false;
if (CollectionUtils.isNotEmpty(userGroups)) {
for (Group group : userGroups) {
if (StringUtils.equalsIgnoreCase(group.getGroupName(), SYSTEM_ADMIN_GROUP)) {
toRet = true;
break;
}
}
}
return toRet;
}
use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.
the class StudioAbstractAccessDecisionVoter method isSiteAdmin.
protected boolean isSiteAdmin(String siteId, User currentUser) {
try {
int total = siteService.getSitesPerUserTotal(currentUser.getUsername());
List<SiteFeed> sitesFeed = siteService.getSitesPerUser(currentUser.getUsername(), 0, total);
Map<String, Long> sites = new HashMap<String, Long>();
for (SiteFeed site : sitesFeed) {
sites.put(site.getSiteId(), site.getId());
}
boolean toRet = sites.containsKey(siteId);
if (toRet) {
List<Group> userGroups = userServiceInternal.getUserGroups(sites.get(siteId), currentUser.getUsername());
for (Group g : userGroups) {
if (g.getGroupName().equals(studioConfiguration.getProperty(CONFIGURATION_DEFAULT_ADMIN_GROUP))) {
toRet = true;
break;
}
}
toRet = userGroups.contains(studioConfiguration.getProperty(CONFIGURATION_DEFAULT_ADMIN_GROUP));
}
return toRet;
} catch (UserNotFoundException e) {
logger.info("User is not site member", e);
return false;
} catch (ServiceLayerException e) {
logger.error("Error getting user memberships", e);
return false;
}
}
use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.
the class GitContentRepository method copyContent.
@Override
public String copyContent(String site, String fromPath, String toPath) {
String commitId = null;
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 = helper.getGitPath(toPath);
try (Git git = new Git(repo)) {
Path sourcePath = Paths.get(repo.getDirectory().getParent(), fromPath);
File sourceFile = sourcePath.toFile();
Path targetPath = Paths.get(repo.getDirectory().getParent(), toPath);
File targetFile = targetPath.toFile();
// Check if we're copying a single file or whole subtree
FileUtils.copyDirectory(sourceFile, targetFile);
// The operation is done on disk, now it's time to commit
git.add().addFilepattern(gitToPath).call();
CommitCommand commitCommand = git.commit().setOnly(gitFromPath).setOnly(gitToPath).setAuthor(helper.getCurrentUserIdent()).setCommitter(helper.getCurrentUserIdent()).setMessage(helper.getCommitMessage(REPO_COPY_CONTENT_COMMIT_MESSAGE).replaceAll(PATTERN_FROM_PATH, fromPath).replaceAll(PATTERN_TO_PATH, toPath));
RevCommit commit = retryingRepositoryOperationFacade.call(commitCommand);
commitId = commit.getName();
}
}
} catch (IOException | GitAPIException | ServiceLayerException | UserNotFoundException | CryptoException e) {
logger.error("Error while copying content for site: " + site + " fromPath: " + fromPath + " toPath: " + toPath + " newName: ");
} finally {
generalLockService.unlock(gitLockKey);
}
return commitId;
}
Aggregations