use of org.craftercms.commons.validation.annotations.param.ValidateStringParam in project studio by craftercms.
the class AwsS3ServiceImpl method listItems.
/**
* {@inheritDoc}
*/
@Override
@HasPermission(type = DefaultPermission.class, action = "s3 read")
public List<S3Item> listItems(@ValidateStringParam(name = "siteId") @ProtectedResourceId("siteId") String siteId, @ValidateStringParam(name = "profileId") String profileId, @ValidateStringParam(name = "path") String path, @ValidateStringParam(name = "type") String type) throws AwsException {
S3Profile profile = getProfile(siteId, profileId);
AmazonS3 client = getS3Client(profile);
List<S3Item> items = new LinkedList<>();
Mimetypes mimetypes = Mimetypes.getInstance();
MimeType filerType = StringUtils.isEmpty(type) || StringUtils.equals(type, ITEM_FILTER) ? MimeTypeUtils.ALL : new MimeType(type);
String prefix = StringUtils.isEmpty(path) ? path : normalizePrefix(path);
ListObjectsV2Request request = new ListObjectsV2Request().withBucketName(profile.getBucketName()).withPrefix(prefix).withDelimiter(delimiter);
ListObjectsV2Result result;
do {
result = client.listObjectsV2(request);
result.getCommonPrefixes().stream().map(p -> new S3Item(StringUtils.removeEnd(StringUtils.removeStart(p, prefix), delimiter), p, true)).forEach(items::add);
result.getObjectSummaries().stream().filter(o -> !StringUtils.equals(o.getKey(), prefix) && MimeType.valueOf(mimetypes.getMimetype(o.getKey())).isCompatibleWith(filerType)).map(o -> new S3Item(StringUtils.removeStart(o.getKey(), prefix), createUrl(profileId, o.getKey()), false)).forEach(items::add);
request.setContinuationToken(result.getNextContinuationToken());
} while (result.isTruncated());
return items;
}
use of org.craftercms.commons.validation.annotations.param.ValidateStringParam 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.commons.validation.annotations.param.ValidateStringParam in project studio by craftercms.
the class WebDavServiceImpl method list.
/**
* {@inheritDoc}
*/
@Override
public List<WebDavItem> list(@ValidateStringParam(name = "site_id") final String site, @ValidateStringParam(name = "profile") final String profileId, @ValidateStringParam(name = "path") final String path, @ValidateStringParam(name = "type") final String type) throws WebDavException {
WebDavProfile profile = getProfile(site, profileId);
String listPath = StringUtils.appendIfMissing(profile.getBaseUrl(), "/");
MimeType filterType;
Sardine sardine = SardineFactory.begin(profile.getUsername(), profile.getPassword());
try {
if (StringUtils.isEmpty(type) || type.equals(FILTER_ALL_ITEMS)) {
filterType = MimeType.valueOf(ALL_VALUE);
} else {
filterType = new MimeType(type);
}
if (StringUtils.isNotEmpty(path)) {
String[] tokens = StringUtils.split(path, "/");
for (String token : tokens) {
if (StringUtils.isNotEmpty(token)) {
listPath += StringUtils.appendIfMissing(UriUtils.encode(token, charset.name()), "/");
}
}
}
if (!sardine.exists(listPath)) {
logger.debug("Folder {0} doesn't exist", listPath);
return Collections.emptyList();
}
String basePath = new URL(profile.getBaseUrl()).getPath();
String baseDomain = profile.getBaseUrl();
String deliveryUrl = profile.getDeliveryBaseUrl();
logger.debug("Listing resources at {0}", listPath);
List<DavResource> resources = sardine.propfind(listPath, 1, properties);
logger.debug("Found {0} resources at {0}", resources.size(), listPath);
return resources.stream().skip(// to avoid repeating the folder being listed
1).filter(r -> r.isDirectory() || filterType.includes(MimeType.valueOf(r.getContentType()))).map(r -> new WebDavItem(getName(r), getUrl(r, baseDomain, deliveryUrl, basePath), r.isDirectory())).collect(Collectors.toList());
} catch (Exception e) {
throw new WebDavException("Error listing resources", e);
}
}
use of org.craftercms.commons.validation.annotations.param.ValidateStringParam in project studio by craftercms.
the class WebDavServiceImpl method list.
/**
* {@inheritDoc}
*/
@Override
@ValidateParams
@HasPermission(type = DefaultPermission.class, action = "webdav_read")
public List<WebDavItem> list(@ValidateStringParam(name = "siteId") @ProtectedResourceId("siteId") final String siteId, @ValidateStringParam(name = "profileId") final String profileId, @ValidateStringParam(name = "path") final String path, @ValidateStringParam(name = "type") final String type) throws WebDavException {
WebDavProfile profile = getProfile(siteId, profileId);
String listPath = StringUtils.appendIfMissing(profile.getBaseUrl(), "/");
MimeType filterType;
try {
Sardine sardine = createClient(profile);
if (StringUtils.isEmpty(type) || type.equals(FILTER_ALL_ITEMS)) {
filterType = MimeType.valueOf(ALL_VALUE);
} else {
filterType = new MimeType(type);
}
if (StringUtils.isNotEmpty(path)) {
String[] tokens = StringUtils.split(path, "/");
for (String token : tokens) {
if (StringUtils.isNotEmpty(token)) {
listPath += StringUtils.appendIfMissing(UriUtils.encode(token, charset.name()), "/");
}
}
}
if (!sardine.exists(listPath)) {
logger.debug("Folder {0} doesn't exist", listPath);
return Collections.emptyList();
}
logger.debug("Listing resources at {0}", listPath);
List<DavResource> resources = sardine.list(listPath, 1, true);
logger.debug("Found {0} resources at {0}", resources.size(), listPath);
return resources.stream().skip(// to avoid repeating the folder being listed
1).filter(r -> r.isDirectory() || filterType.includes(MimeType.valueOf(r.getContentType()))).map(r -> new WebDavItem(getName(r), getUrl(r, profileId, profile), r.isDirectory())).collect(Collectors.toList());
} catch (Exception e) {
throw new WebDavException("Error listing resources", e);
}
}
Aggregations