use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.
the class GitRepositoryHelper method performInitialCommit.
/**
* Perform an initial commit after large changes to a site. Will not work against the global config repo.
* @param site
* @param message
* @return true if successful, false otherwise
*/
public boolean performInitialCommit(String site, String message, String sandboxBranch) {
boolean toReturn = true;
Repository repo = getRepository(site, GitRepositories.SANDBOX, sandboxBranch);
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
generalLockService.lock(gitLockKey);
try (Git git = new Git(repo)) {
Status status = git.status().call();
if (status.hasUncommittedChanges() || !status.isClean()) {
DirCache dirCache = git.add().addFilepattern(GIT_COMMIT_ALL_ITEMS).call();
CommitCommand commitCommand = git.commit().setMessage(message);
String username = securityService.getCurrentUser();
User user = userServiceInternal.getUserByIdOrUsername(-1, username);
if (Objects.nonNull(user)) {
commitCommand = commitCommand.setAuthor(getAuthorIdent(user));
}
RevCommit commit = commitCommand.call();
// TODO: SJ: Do we need the commit id?
// commitId = commit.getName();
}
checkoutSandboxBranch(site, repo, sandboxBranch);
git.close();
} catch (GitAPIException | UserNotFoundException | ServiceLayerException err) {
logger.error("error creating initial commit for site: " + site, err);
toReturn = false;
} finally {
generalLockService.unlock(gitLockKey);
}
return toReturn;
}
use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.
the class SecurityServiceImpl method setUserPassword.
@Override
@ValidateParams
public Map<String, Object> setUserPassword(@ValidateStringParam(name = "token") String token, @ValidateStringParam(name = "newPassword") String newPassword) throws UserNotFoundException, UserExternallyManagedException, ServiceLayerException {
Map<String, Object> toRet = new HashMap<String, Object>();
toRet.put("username", StringUtils.EMPTY);
toRet.put("success", false);
if (validateToken(token)) {
String username = getUsernameFromToken(token);
if (StringUtils.isNotEmpty(username)) {
toRet.put("username", username);
User user = userServiceInternal.getUserByIdOrUsername(-1, username);
if (user != null) {
if (user.isEnabled()) {
toRet.put("success", userServiceInternal.setUserPassword(username, newPassword));
}
} else {
throw new UserNotFoundException("User not found");
}
} else {
throw new UserNotFoundException("User not found");
}
}
return toRet;
}
use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.
the class SecurityServiceImpl method addGlobalUserRoles.
protected void addGlobalUserRoles(String user, Set<String> roles, PermissionsConfigTO rolesConfig) {
try {
List<Group> groups = userServiceInternal.getUserGroups(-1, user);
if (rolesConfig != null && groups != null) {
Map<String, List<String>> rolesMap = rolesConfig.getRoles();
for (Group group : groups) {
String groupName = group.getGroupName();
List<String> userRoles = rolesMap.get(groupName);
if (roles != null && userRoles != null) {
roles.addAll(userRoles);
}
}
}
} catch (ServiceLayerException | UserNotFoundException e) {
logger.error("Unable to retrieve user groups for user {0}", user);
}
}
use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.
the class GroupServiceImpl method addGroupMembers.
@Override
@HasPermission(type = DefaultPermission.class, action = "update_groups")
public List<User> addGroupMembers(long groupId, List<Long> userIds, List<String> usernames) throws ServiceLayerException, UserNotFoundException, GroupNotFoundException, AuthenticationException {
List<User> users = groupServiceInternal.addGroupMembers(groupId, userIds, usernames);
Group group = groupServiceInternal.getGroup(groupId);
SiteFeed siteFeed = siteService.getSite(studioConfiguration.getProperty(CONFIGURATION_GLOBAL_SYSTEM_SITE));
AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
List<AuditLogParameter> parameters = new ArrayList<AuditLogParameter>();
for (User user : users) {
AuditLogParameter parameter = new AuditLogParameter();
parameter.setTargetId(Long.toString(user.getId()));
parameter.setTargetType(TARGET_TYPE_USER);
parameter.setTargetValue(user.getUsername());
parameters.add(parameter);
}
auditLog.setParameters(parameters);
auditLog.setOperation(OPERATION_ADD_MEMBERS);
auditLog.setSiteId(siteFeed.getId());
auditLog.setActorId(userService.getCurrentUser().getUsername());
auditLog.setPrimaryTargetId(Long.toString(groupId));
auditLog.setPrimaryTargetType(TARGET_TYPE_GROUP);
auditLog.setPrimaryTargetValue(group.getGroupName());
auditServiceInternal.insertAuditLog(auditLog);
return users;
}
use of org.craftercms.studio.api.v1.exception.security.UserNotFoundException in project studio by craftercms.
the class HeadersAuthenticationProvider method doAuthenticate.
@Override
public boolean doAuthenticate(HttpServletRequest request, HttpServletResponse response, AuthenticationChain authenticationChain, String username, String password) throws AuthenticationSystemException, UserNotFoundException {
if (isEnabled()) {
logger.debug("Authenticating user using authentication headers.");
RequestContext requestContext = RequestContext.getCurrent();
if (requestContext != null) {
String securekeyHeader = request.getHeader(secureKeyHeader);
logger.debug("Verifying authentication header secure key.");
if (StringUtils.equals(securekeyHeader, secureKeyHeaderValue)) {
String usernameHeaderValue = request.getHeader(usernameHeader);
String firstName = request.getHeader(firstNameHeader);
String lastName = request.getHeader(lastNameHeader);
String email = request.getHeader(emailHeader);
String groups = request.getHeader(groupsHeader);
try {
UserServiceInternal userServiceInternal = authenticationChain.getUserServiceInternal();
AuditServiceInternal auditServiceInternal = authenticationChain.getAuditServiceInternal();
StudioConfiguration studioConfiguration = authenticationChain.getStudioConfiguration();
SiteService siteService = authenticationChain.getSiteService();
SiteFeed siteFeed = siteService.getSite(studioConfiguration.getProperty(CONFIGURATION_GLOBAL_SYSTEM_SITE));
if (userServiceInternal.userExists(-1, usernameHeaderValue)) {
User user = userServiceInternal.getUserByIdOrUsername(-1, usernameHeaderValue);
user.setFirstName(firstName);
user.setLastName(lastName);
user.setEmail(email);
if (StringUtils.isNoneEmpty(firstName, lastName, email)) {
logger.debug("If user already exists in studio DB, update details.");
try {
userServiceInternal.updateUser(user);
AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
auditLog.setOperation(OPERATION_UPDATE);
auditLog.setActorId(usernameHeaderValue);
auditLog.setSiteId(siteFeed.getId());
auditLog.setPrimaryTargetId(usernameHeaderValue);
auditLog.setPrimaryTargetType(TARGET_TYPE_USER);
auditLog.setPrimaryTargetValue(user.getUsername());
auditServiceInternal.insertAuditLog(auditLog);
} catch (Exception e) {
logger.debug("Error updating user " + usernameHeaderValue + " with data from authentication headers", e);
throw new AuthenticationSystemException("Error updating user " + usernameHeaderValue + " with data from " + "external authentication provider", e);
}
}
} else {
logger.debug("User does not exist in studio db. Adding user " + usernameHeader);
try {
User user = new User();
user.setUsername(usernameHeaderValue);
user.setPassword(UUID.randomUUID().toString());
user.setFirstName(firstName);
user.setLastName(lastName);
user.setEmail(email);
user.setExternallyManaged(true);
user.setEnabled(true);
userServiceInternal.createUser(user);
AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
auditLog.setOperation(OPERATION_CREATE);
auditLog.setSiteId(siteFeed.getId());
auditLog.setActorId(usernameHeaderValue);
auditLog.setPrimaryTargetId(usernameHeaderValue);
auditLog.setPrimaryTargetType(TARGET_TYPE_USER);
auditLog.setPrimaryTargetValue(user.getUsername());
auditServiceInternal.insertAuditLog(auditLog);
} catch (UserAlreadyExistsException | ServiceLayerException e) {
logger.debug("Error adding user " + usernameHeaderValue + " from authentication " + "headers", e);
throw new AuthenticationSystemException("Error adding user " + usernameHeaderValue + " from external " + "authentication provider", e);
}
}
} catch (ServiceLayerException e) {
logger.debug("Unknown service error", e);
throw new AuthenticationSystemException("Unknown service error", e);
}
User user = new User();
user.setUsername(usernameHeaderValue);
user.setFirstName(firstName);
user.setLastName(lastName);
user.setEmail(email);
user.setGroups(new ArrayList<UserGroup>());
logger.debug("Update user groups in database.");
if (StringUtils.isNoneEmpty(groups)) {
String[] groupsArray = groups.split(",");
for (int i = 0; i < groupsArray.length; i++) {
Group g = new Group();
try {
g.setGroupName(StringUtils.trim(groupsArray[i]));
g.setGroupDescription("Externally managed group");
g.setOrganization(null);
UserGroup ug = new UserGroup();
ug.setGroup(g);
user.getGroups().add(ug);
upsertUserGroup(g.getGroupName(), usernameHeaderValue, authenticationChain);
} catch (Exception e) {
logger.debug("Error updating user group " + g.getGroupName() + " with data from authentication headers", e);
}
}
}
String token = createToken(user, authenticationChain);
if (isLogoutEnabled()) {
storeAuthentication(new Authentication(usernameHeaderValue, token, AuthenticationType.AUTH_HEADERS, logoutUrl));
} else {
storeAuthentication(new Authentication(usernameHeaderValue, token, AuthenticationType.AUTH_HEADERS));
}
return true;
}
}
logger.debug("Unable to authenticate user using authentication headers");
return false;
} else {
logger.debug("Authentication using headers disabled");
return false;
}
}
Aggregations