Search in sources :

Example 16 with UserNotFoundException

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;
}
Also used : UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) User(org.craftercms.studio.api.v2.dal.User) InputStream(java.io.InputStream) JSONException(net.sf.json.JSONException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) JSONObject(net.sf.json.JSONObject) FilterInvocation(org.springframework.security.web.FilterInvocation)

Example 17 with UserNotFoundException

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;
    }
}
Also used : UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) HashSet(java.util.HashSet)

Example 18 with UserNotFoundException

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;
}
Also used : UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) Group(org.craftercms.studio.api.v2.dal.Group) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException)

Example 19 with UserNotFoundException

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;
    }
}
Also used : UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) Group(org.craftercms.studio.api.v2.dal.Group) HashMap(java.util.HashMap) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException)

Example 20 with UserNotFoundException

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;
}
Also used : Path(java.nio.file.Path) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) IOException(java.io.IOException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Git(org.eclipse.jgit.api.Git) CommitCommand(org.eclipse.jgit.api.CommitCommand) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) CryptoException(org.craftercms.commons.crypto.CryptoException) File(java.io.File) LockFile(org.eclipse.jgit.internal.storage.file.LockFile) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

UserNotFoundException (org.craftercms.studio.api.v1.exception.security.UserNotFoundException)43 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)40 User (org.craftercms.studio.api.v2.dal.User)32 IOException (java.io.IOException)15 HashMap (java.util.HashMap)15 ArrayList (java.util.ArrayList)11 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)11 UserExternallyManagedException (org.craftercms.studio.api.v1.exception.security.UserExternallyManagedException)11 Group (org.craftercms.studio.api.v2.dal.Group)11 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)11 Repository (org.eclipse.jgit.lib.Repository)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 UserAlreadyExistsException (org.craftercms.studio.api.v1.exception.security.UserAlreadyExistsException)10 Git (org.eclipse.jgit.api.Git)10 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)10 AuditLog (org.craftercms.studio.api.v2.dal.AuditLog)9 GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)9 CryptoException (org.craftercms.commons.crypto.CryptoException)8 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)8 PasswordDoesNotMatchException (org.craftercms.studio.api.v1.exception.security.PasswordDoesNotMatchException)7