use of org.opencastproject.security.api.User in project opencast by opencast.
the class GroupsEndpoint method membersToJSON.
/**
* Generate a JSON array based on the given set of members
*
* @param members
* the members source
* @return a JSON array ({@link JValue}) with the given members
*/
private JValue membersToJSON(Set<String> members) {
List<JValue> membersJSON = new ArrayList<>();
for (String username : members) {
User user = userDirectoryService.loadUser(username);
String name = username;
if (user != null && StringUtils.isNotBlank(user.getName())) {
name = user.getName();
}
membersJSON.add(obj(f("username", v(username)), f("name", v(name))));
}
return arr(membersJSON);
}
use of org.opencastproject.security.api.User in project opencast by opencast.
the class ThemesEndpoint method createTheme.
@POST
@Path("")
@RestQuery(name = "createTheme", description = "Add a theme", returnDescription = "Return the created theme", restParameters = { @RestParameter(name = "default", description = "Whether the theme is default", isRequired = true, type = Type.BOOLEAN), @RestParameter(name = "name", description = "The theme name", isRequired = true, type = Type.STRING), @RestParameter(name = "description", description = "The theme description", isRequired = false, type = Type.TEXT), @RestParameter(name = "bumperActive", description = "Whether the theme bumper is active", isRequired = false, type = Type.BOOLEAN), @RestParameter(name = "trailerActive", description = "Whether the theme trailer is active", isRequired = false, type = Type.BOOLEAN), @RestParameter(name = "titleSlideActive", description = "Whether the theme title slide is active", isRequired = false, type = Type.BOOLEAN), @RestParameter(name = "licenseSlideActive", description = "Whether the theme license slide is active", isRequired = false, type = Type.BOOLEAN), @RestParameter(name = "watermarkActive", description = "Whether the theme watermark is active", isRequired = false, type = Type.BOOLEAN), @RestParameter(name = "bumperFile", description = "The theme bumper file", isRequired = false, type = Type.STRING), @RestParameter(name = "trailerFile", description = "The theme trailer file", isRequired = false, type = Type.STRING), @RestParameter(name = "watermarkFile", description = "The theme watermark file", isRequired = false, type = Type.STRING), @RestParameter(name = "titleSlideBackground", description = "The theme title slide background file", isRequired = false, type = Type.STRING), @RestParameter(name = "licenseSlideBackground", description = "The theme license slide background file", isRequired = false, type = Type.STRING), @RestParameter(name = "titleSlideMetadata", description = "The theme title slide metadata", isRequired = false, type = Type.STRING), @RestParameter(name = "licenseSlideDescription", description = "The theme license slide description", isRequired = false, type = Type.STRING), @RestParameter(name = "watermarkPosition", description = "The theme watermark position", isRequired = false, type = Type.STRING) }, reponses = { @RestResponse(responseCode = SC_OK, description = "Theme created"), @RestResponse(responseCode = SC_BAD_REQUEST, description = "The theme references a non-existing file") })
public Response createTheme(@FormParam("default") boolean isDefault, @FormParam("name") String name, @FormParam("description") String description, @FormParam("bumperActive") Boolean bumperActive, @FormParam("trailerActive") Boolean trailerActive, @FormParam("titleSlideActive") Boolean titleSlideActive, @FormParam("licenseSlideActive") Boolean licenseSlideActive, @FormParam("watermarkActive") Boolean watermarkActive, @FormParam("bumperFile") String bumperFile, @FormParam("trailerFile") String trailerFile, @FormParam("watermarkFile") String watermarkFile, @FormParam("titleSlideBackground") String titleSlideBackground, @FormParam("licenseSlideBackground") String licenseSlideBackground, @FormParam("titleSlideMetadata") String titleSlideMetadata, @FormParam("licenseSlideDescription") String licenseSlideDescription, @FormParam("watermarkPosition") String watermarkPosition) {
User creator = securityService.getUser();
Theme theme = new Theme(Option.<Long>none(), new Date(), isDefault, creator, name, StringUtils.trimToNull(description), BooleanUtils.toBoolean(bumperActive), StringUtils.trimToNull(bumperFile), BooleanUtils.toBoolean(trailerActive), StringUtils.trimToNull(trailerFile), BooleanUtils.toBoolean(titleSlideActive), StringUtils.trimToNull(titleSlideMetadata), StringUtils.trimToNull(titleSlideBackground), BooleanUtils.toBoolean(licenseSlideActive), StringUtils.trimToNull(licenseSlideBackground), StringUtils.trimToNull(licenseSlideDescription), BooleanUtils.toBoolean(watermarkActive), StringUtils.trimToNull(watermarkFile), StringUtils.trimToNull(watermarkPosition));
try {
persistReferencedFiles(theme);
} catch (NotFoundException e) {
logger.warn("A file that is referenced in theme '{}' was not found: {}", theme, e.getMessage());
return R.badRequest("Referenced non-existing file");
} catch (IOException e) {
logger.warn("Error while persisting file: {}", e.getMessage());
return R.serverError();
}
try {
Theme createdTheme = themesServiceDatabase.updateTheme(theme);
return RestUtils.okJson(themeToJSON(createdTheme));
} catch (ThemesServiceDatabaseException e) {
logger.error("Unable to create a theme");
return RestUtil.R.serverError();
}
}
use of org.opencastproject.security.api.User in project opencast by opencast.
the class UsersEndpoint method createUser.
@POST
@Path("/")
@RestQuery(name = "createUser", description = "Create a new user", returnDescription = "The location of the new ressource", restParameters = { @RestParameter(description = "The username.", isRequired = true, name = "username", type = STRING), @RestParameter(description = "The password.", isRequired = true, name = "password", type = STRING), @RestParameter(description = "The name.", isRequired = false, name = "name", type = STRING), @RestParameter(description = "The email.", isRequired = false, name = "email", type = STRING), @RestParameter(name = "roles", type = STRING, isRequired = false, description = "The user roles as a json array") }, reponses = { @RestResponse(responseCode = SC_CREATED, description = "User has been created."), @RestResponse(responseCode = SC_FORBIDDEN, description = "Not enough permissions to create a user with a admin role."), @RestResponse(responseCode = SC_CONFLICT, description = "An user with this username already exist.") })
public Response createUser(@FormParam("username") String username, @FormParam("password") String password, @FormParam("name") String name, @FormParam("email") String email, @FormParam("roles") String roles) throws NotFoundException {
if (StringUtils.isBlank(username))
return RestUtil.R.badRequest("No username set");
if (StringUtils.isBlank(password))
return RestUtil.R.badRequest("No password set");
User existingUser = jpaUserAndRoleProvider.loadUser(username);
if (existingUser != null) {
return Response.status(SC_CONFLICT).build();
}
JpaOrganization organization = (JpaOrganization) securityService.getOrganization();
Option<JSONArray> rolesArray = Option.none();
if (StringUtils.isNotBlank(roles)) {
rolesArray = Option.option((JSONArray) JSONValue.parse(roles));
}
Set<JpaRole> rolesSet = new HashSet<>();
// Add the roles given
if (rolesArray.isSome()) {
// Add the roles given
for (Object role : rolesArray.get()) {
JSONObject roleAsJson = (JSONObject) role;
Role.Type roletype = Role.Type.valueOf((String) roleAsJson.get("type"));
rolesSet.add(new JpaRole(roleAsJson.get("id").toString(), organization, null, roletype));
}
} else {
rolesSet.add(new JpaRole(organization.getAnonymousRole(), organization));
}
JpaUser user = new JpaUser(username, password, organization, name, email, jpaUserAndRoleProvider.getName(), true, rolesSet);
try {
jpaUserAndRoleProvider.addUser(user);
return Response.created(uri(endpointBaseUrl, user.getUsername() + ".json")).build();
} catch (UnauthorizedException e) {
return Response.status(Response.Status.FORBIDDEN).build();
}
}
use of org.opencastproject.security.api.User in project opencast by opencast.
the class EventCommentParser method replyFromManifest.
private static EventCommentReply replyFromManifest(Node commentReplyNode, UserDirectoryService userDirectoryService) throws UnsupportedElementException {
try {
// id
Long id = null;
Double idAsDouble = ((Number) xpath.evaluate("@id", commentReplyNode, XPathConstants.NUMBER)).doubleValue();
if (!idAsDouble.isNaN())
id = idAsDouble.longValue();
// text
String text = (String) xpath.evaluate("text/text()", commentReplyNode, XPathConstants.STRING);
// Author
Node authorNode = (Node) xpath.evaluate("author", commentReplyNode, XPathConstants.NODE);
User author = userFromManifest(authorNode, userDirectoryService);
// CreationDate
String creationDateString = (String) xpath.evaluate("creationDate/text()", commentReplyNode, XPathConstants.STRING);
Date creationDate = new Date(DateTimeSupport.fromUTC(creationDateString));
// ModificationDate
String modificationDateString = (String) xpath.evaluate("modificationDate/text()", commentReplyNode, XPathConstants.STRING);
Date modificationDate = new Date(DateTimeSupport.fromUTC(modificationDateString));
// Create reply
return EventCommentReply.create(Option.option(id), text.trim(), author, creationDate, modificationDate);
} catch (XPathExpressionException e) {
throw new UnsupportedElementException("Error while reading comment reply information from manifest", e);
} catch (Exception e) {
if (e instanceof UnsupportedElementException)
throw (UnsupportedElementException) e;
throw new UnsupportedElementException("Error while reading comment reply creation or modification date information from manifest", e);
}
}
use of org.opencastproject.security.api.User in project opencast by opencast.
the class OaiPmhPersistenceTest method setUp.
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
// Mock up a security service
SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
User user = SecurityUtil.createSystemUser("admin", new DefaultOrganization());
EasyMock.expect(securityService.getOrganization()).andReturn(new DefaultOrganization()).anyTimes();
EasyMock.expect(securityService.getUser()).andReturn(user).anyTimes();
EasyMock.replay(securityService);
mp1 = MediaPackageSupport.loadFromClassPath("/mp1.xml");
mp2 = MediaPackageSupport.loadFromClassPath("/mp2.xml");
Workspace workspace = EasyMock.createNiceMock(Workspace.class);
EasyMock.expect(workspace.read(uri("series-dublincore.xml"))).andAnswer(() -> getClass().getResourceAsStream("/series-dublincore.xml")).anyTimes();
EasyMock.expect(workspace.read(uri("episode-dublincore.xml"))).andAnswer(() -> getClass().getResourceAsStream("/episode-dublincore.xml")).anyTimes();
EasyMock.expect(workspace.read(uri("mpeg7.xml"))).andAnswer(() -> getClass().getResourceAsStream("/mpeg7.xml")).anyTimes();
EasyMock.expect(workspace.read(uri("series-xacml.xml"))).andAnswer(() -> getClass().getResourceAsStream("/series-xacml.xml")).anyTimes();
EasyMock.replay(workspace);
oaiPmhDatabase = new OaiPmhDatabaseImpl();
oaiPmhDatabase.setEntityManagerFactory(newTestEntityManagerFactory(OaiPmhDatabaseImpl.PERSISTENCE_UNIT_NAME));
oaiPmhDatabase.setSecurityService(securityService);
oaiPmhDatabase.setWorkspace(workspace);
oaiPmhDatabase.activate(null);
}
Aggregations