Search in sources :

Example 66 with SiteNotFoundException

use of org.craftercms.studio.api.v1.exception.SiteNotFoundException in project studio by craftercms.

the class HeadersAuthenticationProvider method upsertUserGroup.

protected boolean upsertUserGroup(String groupName, String username, AuthenticationChain authenticationChain) throws SiteNotFoundException {
    GroupDAO groupDao = authenticationChain.getGroupDao();
    UserDAO userDao = authenticationChain.getUserDao();
    AuditServiceInternal auditServiceInternal = authenticationChain.getAuditServiceInternal();
    SiteService siteService = authenticationChain.getSiteService();
    StudioConfiguration studioConfiguration = authenticationChain.getStudioConfiguration();
    SiteFeed siteFeed = siteService.getSite(studioConfiguration.getProperty(CONFIGURATION_GLOBAL_SYSTEM_SITE));
    try {
        Map<String, Object> params = new HashMap<>();
        params.put(ORG_ID, DEFAULT_ORGANIZATION_ID);
        params.put(GROUP_NAME, groupName);
        params.put(GROUP_DESCRIPTION, "Externally managed group - " + groupName);
        groupDao.createGroup(params);
    } catch (Exception e) {
        logger.debug("Error creating group", e);
    }
    Map<String, Object> params = new HashMap<String, Object>();
    params.put(GROUP_NAME, groupName);
    Group group = groupDao.getGroupByName(params);
    if (group != null) {
        List<String> usernames = new ArrayList<String>();
        params = new HashMap<>();
        params.put(USER_ID, -1);
        params.put(USERNAME, username);
        User user = userDao.getUserByIdOrUsername(params);
        List<Long> users = new ArrayList<Long>();
        users.add(user.getId());
        params = new HashMap<>();
        params.put(USER_IDS, users);
        params.put(GROUP_ID, group.getId());
        try {
            groupDao.addGroupMembers(params);
            AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
            auditLog.setOperation(OPERATION_ADD_MEMBERS);
            auditLog.setSiteId(siteFeed.getId());
            auditLog.setActorId(username);
            auditLog.setPrimaryTargetId(group.getGroupName() + ":" + user.getUsername());
            auditLog.setPrimaryTargetType(TARGET_TYPE_USER);
            auditLog.setPrimaryTargetValue(user.getUsername());
            auditServiceInternal.insertAuditLog(auditLog);
        } catch (Exception e) {
            logger.debug("Unknown database error", e);
        }
    }
    return true;
}
Also used : UserGroup(org.craftercms.studio.api.v2.dal.UserGroup) Group(org.craftercms.studio.api.v2.dal.Group) User(org.craftercms.studio.api.v2.dal.User) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UserAlreadyExistsException(org.craftercms.studio.api.v1.exception.security.UserAlreadyExistsException) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) AuthenticationSystemException(org.craftercms.studio.api.v1.exception.security.AuthenticationSystemException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) AuditLog(org.craftercms.studio.api.v2.dal.AuditLog) StudioConfiguration(org.craftercms.studio.api.v2.utils.StudioConfiguration) AuditServiceInternal(org.craftercms.studio.api.v2.service.audit.internal.AuditServiceInternal) UserDAO(org.craftercms.studio.api.v2.dal.UserDAO) SiteService(org.craftercms.studio.api.v1.service.site.SiteService) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) GroupDAO(org.craftercms.studio.api.v2.dal.GroupDAO)

Example 67 with SiteNotFoundException

use of org.craftercms.studio.api.v1.exception.SiteNotFoundException in project studio by craftercms.

the class UserServiceImpl method getUserSites.

@Override
@HasPermission(type = DefaultPermission.class, action = "read_users")
public List<Site> getUserSites(long userId, String username) throws ServiceLayerException, UserNotFoundException {
    List<Site> sites = new ArrayList<>();
    Set<String> allSites = siteService.getAllAvailableSites();
    List<Group> userGroups = userServiceInternal.getUserGroups(userId, username);
    boolean isSysAdmin = userGroups.stream().anyMatch(group -> group.getGroupName().equals(SYSTEM_ADMIN_GROUP));
    // Iterate all sites. If the user has any of the site groups, it has access to the site
    for (String siteId : allSites) {
        List<String> siteGroups = groupServiceInternal.getSiteGroups(siteId);
        if (isSysAdmin || userGroups.stream().anyMatch(userGroup -> siteGroups.contains(userGroup.getGroupName()))) {
            try {
                SiteFeed siteFeed = siteService.getSite(siteId);
                Site site = new Site();
                site.setSiteId(siteFeed.getSiteId());
                site.setDesc(siteFeed.getDescription());
                sites.add(site);
            } catch (SiteNotFoundException e) {
                logger.error("Site not found: {0}", e, siteId);
            }
        }
    }
    return sites;
}
Also used : Site(org.craftercms.studio.model.Site) Arrays(java.util.Arrays) AuditLogParameter(org.craftercms.studio.api.v2.dal.AuditLogParameter) UserServiceInternal(org.craftercms.studio.api.v2.service.security.internal.UserServiceInternal) TextEncryptor(org.craftercms.commons.crypto.TextEncryptor) ZonedDateTime(java.time.ZonedDateTime) MessagingException(javax.mail.MessagingException) FreeMarkerConfig(org.springframework.web.servlet.view.freemarker.FreeMarkerConfig) StringUtils(org.apache.commons.lang3.StringUtils) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) User(org.craftercms.studio.api.v2.dal.User) AuditServiceInternal(org.craftercms.studio.api.v2.service.audit.internal.AuditServiceInternal) SECURITY_FORGOT_PASSWORD_EMAIL_TEMPLATE(org.craftercms.studio.api.v2.utils.StudioConfiguration.SECURITY_FORGOT_PASSWORD_EMAIL_TEMPLATE) Map(java.util.Map) AuditLog(org.craftercms.studio.api.v2.dal.AuditLog) SECURITY_FORGOT_PASSWORD_MESSAGE_SUBJECT(org.craftercms.studio.api.v2.utils.StudioConfiguration.SECURITY_FORGOT_PASSWORD_MESSAGE_SUBJECT) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) Collection(java.util.Collection) Set(java.util.Set) SiteService(org.craftercms.studio.api.v1.service.site.SiteService) JavaMailSender(org.springframework.mail.javamail.JavaMailSender) CONFIGURATION_GLOBAL_SYSTEM_SITE(org.craftercms.studio.api.v2.utils.StudioConfiguration.CONFIGURATION_GLOBAL_SYSTEM_SITE) GroupServiceInternal(org.craftercms.studio.api.v2.service.security.internal.GroupServiceInternal) Site(org.craftercms.studio.model.Site) SECURITY_RESET_PASSWORD_SERVICE_URL(org.craftercms.studio.api.v2.utils.StudioConfiguration.SECURITY_RESET_PASSWORD_SERVICE_URL) EntitlementValidator(org.craftercms.commons.entitlements.validator.EntitlementValidator) StandardCharsets(java.nio.charset.StandardCharsets) AuthenticatedUser(org.craftercms.studio.model.AuthenticatedUser) EntitlementType(org.craftercms.commons.entitlements.model.EntitlementType) Base64(java.util.Base64) List(java.util.List) StudioConfiguration(org.craftercms.studio.api.v2.utils.StudioConfiguration) Writer(java.io.Writer) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) OPERATION_CREATE(org.craftercms.studio.api.v2.dal.AuditLogConstants.OPERATION_CREATE) DefaultPermission(org.craftercms.commons.security.permissions.DefaultPermission) SYSTEM_ADMIN_GROUP(org.craftercms.studio.api.v1.constant.StudioConstants.SYSTEM_ADMIN_GROUP) PasswordDoesNotMatchException(org.craftercms.studio.api.v1.exception.security.PasswordDoesNotMatchException) UserAlreadyExistsException(org.craftercms.studio.api.v1.exception.security.UserAlreadyExistsException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) TARGET_TYPE_USER(org.craftercms.studio.api.v2.dal.AuditLogConstants.TARGET_TYPE_USER) RequestContext(org.craftercms.commons.http.RequestContext) TemplateException(freemarker.template.TemplateException) Logger(org.craftercms.studio.api.v1.log.Logger) OPERATION_DELETE(org.craftercms.studio.api.v2.dal.AuditLogConstants.OPERATION_DELETE) HashMap(java.util.HashMap) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) StringTokenizer(java.util.StringTokenizer) SECURITY_FORGOT_PASSWORD_TOKEN_TIMEOUT(org.craftercms.studio.api.v2.utils.StudioConfiguration.SECURITY_FORGOT_PASSWORD_TOKEN_TIMEOUT) LoggerFactory(org.craftercms.studio.api.v1.log.LoggerFactory) EntitlementException(org.craftercms.commons.entitlements.exception.EntitlementException) Template(freemarker.template.Template) GroupNotFoundException(org.craftercms.studio.api.v1.exception.security.GroupNotFoundException) OPERATION_UPDATE(org.craftercms.studio.api.v2.dal.AuditLogConstants.OPERATION_UPDATE) UserExternallyManagedException(org.craftercms.studio.api.v1.exception.security.UserExternallyManagedException) LinkedHashSet(java.util.LinkedHashSet) ConfigurationService(org.craftercms.studio.api.v2.service.config.ConfigurationService) MapUtils(org.apache.commons.collections4.MapUtils) UserService(org.craftercms.studio.api.v2.service.security.UserService) StringWriter(java.io.StringWriter) REMOVE_SYSTEM_ADMIN_MEMBER_LOCK(org.craftercms.studio.api.v1.constant.StudioConstants.REMOVE_SYSTEM_ADMIN_MEMBER_LOCK) HasPermission(org.craftercms.commons.security.permissions.annotations.HasPermission) OPERATION_DISABLE(org.craftercms.studio.api.v2.dal.AuditLogConstants.OPERATION_DISABLE) IOException(java.io.IOException) MimeMessage(javax.mail.internet.MimeMessage) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) CryptoException(org.craftercms.commons.crypto.CryptoException) OPERATION_ENABLE(org.craftercms.studio.api.v2.dal.AuditLogConstants.OPERATION_ENABLE) PermissionException(org.craftercms.commons.security.exception.PermissionException) ObjectFactory(org.springframework.beans.factory.ObjectFactory) Group(org.craftercms.studio.api.v2.dal.Group) SecurityService(org.craftercms.studio.api.v1.service.security.SecurityService) GeneralLockService(org.craftercms.studio.api.v1.service.GeneralLockService) MAIL_FROM_DEFAULT(org.craftercms.studio.api.v2.utils.StudioConfiguration.MAIL_FROM_DEFAULT) InstanceService(org.craftercms.studio.api.v2.service.system.InstanceService) MAIL_SMTP_AUTH(org.craftercms.studio.api.v2.utils.StudioConfiguration.MAIL_SMTP_AUTH) AuthenticationException(org.craftercms.studio.api.v1.exception.security.AuthenticationException) Collections(java.util.Collections) Group(org.craftercms.studio.api.v2.dal.Group) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) ArrayList(java.util.ArrayList) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) HasPermission(org.craftercms.commons.security.permissions.annotations.HasPermission)

Example 68 with SiteNotFoundException

use of org.craftercms.studio.api.v1.exception.SiteNotFoundException in project studio by craftercms.

the class SiteRepositoryUpgradePipelineImpl method execute.

/**
 * {@inheritDoc}
 */
@Override
public void execute(final String site) throws UpgradeException {
    String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
    generalLockService.lock(gitLockKey);
    try {
        clusterSandboxRepoSyncTask.execute(site);
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        Repository repository = helper.getRepository(site, GitRepositories.SANDBOX);
        String sandboxBranch = siteSandboxBranch;
        if (repository != null) {
            Git git = new Git(repository);
            try {
                if (!isEmpty()) {
                    SiteFeed siteFeed = siteService.getSite(site);
                    if (!StringUtils.isEmpty(siteFeed.getSandboxBranch())) {
                        sandboxBranch = siteFeed.getSandboxBranch();
                    }
                    createTemporaryBranch(site, git);
                    checkoutBranch(siteUpgradeBranch, git);
                    super.execute(site);
                    checkoutBranch(sandboxBranch, git);
                    mergeTemporaryBranch(repository, git);
                    deleteTemporaryBranch(git);
                }
            } catch (GitAPIException | IOException | SiteNotFoundException e) {
                throw new UpgradeException("Error branching or merging upgrade branch for site " + site, e);
            } finally {
                if (!isEmpty()) {
                    try {
                        checkoutBranch(sandboxBranch, git);
                    } catch (GitAPIException e) {
                        logger.error("Error cleaning up repo for site " + site, e);
                    }
                }
                git.close();
            }
        }
    } catch (CryptoException e) {
        throw new UpgradeException("Unexpected error upgrading site " + site, e);
    } finally {
        generalLockService.unlock(gitLockKey);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) UpgradeException(org.craftercms.studio.api.v2.exception.UpgradeException) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) IOException(java.io.IOException) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) CryptoException(org.craftercms.commons.crypto.CryptoException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException)

Aggregations

SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)41 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)34 AuditLog (org.craftercms.studio.api.v2.dal.AuditLog)23 ResponseBody (org.craftercms.studio.model.rest.ResponseBody)20 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)13 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)12 PostMapping (org.springframework.web.bind.annotation.PostMapping)11 ValidateParams (org.craftercms.commons.validation.annotations.param.ValidateParams)10 ContentItemTO (org.craftercms.studio.api.v1.to.ContentItemTO)9 GetMapping (org.springframework.web.bind.annotation.GetMapping)9 ClusterMember (org.craftercms.studio.api.v2.dal.ClusterMember)8 Result (org.craftercms.studio.model.rest.Result)8 CryptoException (org.craftercms.commons.crypto.CryptoException)6 ResultOne (org.craftercms.studio.model.rest.ResultOne)6 IOException (java.io.IOException)5 Set (java.util.Set)5 ItemMetadata (org.craftercms.studio.api.v1.dal.ItemMetadata)5 AuditLogParameter (org.craftercms.studio.api.v2.dal.AuditLogParameter)5 HashSet (java.util.HashSet)4