Search in sources :

Example 1 with Site

use of org.craftercms.studio.model.Site in project studio by craftercms.

the class UsersController method getUserSites.

/**
 * Get user sites API
 *
 * @param userId User identifier
 * @return Response containing list of sites
 */
@GetMapping(PATH_PARAM_ID + SITES)
public ResponseBody getUserSites(@PathVariable(REQUEST_PARAM_ID) String userId, @RequestParam(value = REQUEST_PARAM_OFFSET, required = false, defaultValue = "0") int offset, @RequestParam(value = REQUEST_PARAM_LIMIT, required = false, defaultValue = "10") int limit) throws ServiceLayerException, UserNotFoundException {
    int uId = -1;
    String username = StringUtils.EMPTY;
    if (StringUtils.isNumeric(userId)) {
        uId = Integer.parseInt(userId);
    } else {
        username = userId;
    }
    List<Site> allSites = userService.getUserSites(uId, username);
    List<Site> paginatedSites = PaginationUtils.paginate(allSites, offset, limit, "siteId");
    PaginatedResultList<Site> result = new PaginatedResultList<>();
    result.setResponse(OK);
    result.setTotal(allSites.size());
    result.setOffset(offset);
    result.setLimit(limit);
    result.setEntities(RESULT_KEY_SITES, paginatedSites);
    ResponseBody responseBody = new ResponseBody();
    responseBody.setResult(result);
    return responseBody;
}
Also used : Site(org.craftercms.studio.model.Site) PaginatedResultList(org.craftercms.studio.model.rest.PaginatedResultList) ResponseBody(org.craftercms.studio.model.rest.ResponseBody) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 2 with Site

use of org.craftercms.studio.model.Site in project studio by craftercms.

the class UsersController method getCurrentUserSites.

/**
 * Get the sites of the current authenticated user API
 *
 * @return Response containing current authenticated user sites
 */
@GetMapping(ME + SITES)
public ResponseBody getCurrentUserSites(@RequestParam(value = REQUEST_PARAM_OFFSET, required = false, defaultValue = "0") int offset, @RequestParam(value = REQUEST_PARAM_LIMIT, required = false, defaultValue = "10") int limit) throws AuthenticationException, ServiceLayerException {
    List<Site> allSites = userService.getCurrentUserSites();
    List<Site> paginatedSites = PaginationUtils.paginate(allSites, offset, limit, "siteId");
    PaginatedResultList<Site> result = new PaginatedResultList<>();
    result.setResponse(OK);
    result.setTotal(allSites.size());
    result.setOffset(offset);
    result.setLimit(limit);
    result.setEntities(RESULT_KEY_SITES, paginatedSites);
    ResponseBody responseBody = new ResponseBody();
    responseBody.setResult(result);
    return responseBody;
}
Also used : Site(org.craftercms.studio.model.Site) PaginatedResultList(org.craftercms.studio.model.rest.PaginatedResultList) ResponseBody(org.craftercms.studio.model.rest.ResponseBody) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 3 with Site

use of org.craftercms.studio.model.Site 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)

Aggregations

Site (org.craftercms.studio.model.Site)3 PaginatedResultList (org.craftercms.studio.model.rest.PaginatedResultList)2 ResponseBody (org.craftercms.studio.model.rest.ResponseBody)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 Template (freemarker.template.Template)1 TemplateException (freemarker.template.TemplateException)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ZonedDateTime (java.time.ZonedDateTime)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Base64 (java.util.Base64)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1