@Override public Tenant findTenant(String slug) { return this.tenantStore.get().findBySlug(slug); }
@Override public Integer countAllTenants() { return this.tenantStore.get().countAll(); }
@Override public User findUserByEmailOrUserName(String userNameOrEmail) { return this.userStore.get().findUserByEmailOrUserName(userNameOrEmail); }
public <E extends Entity> void load(EntityData<E> entity, LoadingOption... options) { if (webContext.getTenant() != null) { entity.setData(Tenant.class, webContext.getTenant()); } else { if (Owned.class.isAssignableFrom(entity.getEntity().getClass())) { Owned owned = (Owned) entity.getEntity(); entity.setData(Tenant.class, this.tenantStore.get().findById(owned.getTenantId())); } } }
@Override public Tenant createDefaultTenant() throws EntityAlreadyExistsException { if (this.tenantStore.get().findAll(1, 0).size() != 0) { throw new EntityAlreadyExistsException("Cannot create default tenant : a tenant already exists"); } String slug = multitenancySettings.getDefaultTenantSlug(); TenantConfiguration configuration = new TenantConfiguration(); Tenant tenant = new Tenant(slug, configuration); try { this.tenantStore.get().create(tenant); } catch (InvalidEntityException e) { } return this.tenantStore.get().findBySlug(slug); }
@Override public void createPasswordResetRequest(String emailOrUsername) throws UserNotFoundException { Preconditions.checkNotNull(emailOrUsername); User user = this.userStore.get().findUserByEmailOrUserName(emailOrUsername); if (user == null) { throw new UserNotFoundException("No user found with this email or usernmae"); } AccountsSettings settings = getSettings(); final String secret = generateSecret(); this.userStore.get().createPasswordResetRequest(user, secret); sendPasswordResetMail(user, secret, settings); }
@Override public void resetPassword(String resetKey, String password) throws NoSuchPasswordResetKeyException, PasswordDoesNotMeetRequirementsException { Preconditions.checkNotNull(resetKey); Preconditions.checkNotNull(password); User user = this.userStore.get().findUserByPasswordResetRequest(resetKey); if (user == null) { throw new NoSuchPasswordResetKeyException(); } AccountsSettings settings = getSettings(); if (password.length() < settings.getPasswordRequirements().getMinimalLength()) { throw new PasswordDoesNotMeetRequirementsException("Provided password does not meet requirements"); } this.userStore.get().updatePassword(user, passwordManager.hashPassword(password)); this.userStore.get().deletePasswordResetRequest(resetKey); }
@Override public List<Role> findRolesForUser(User user) { return this.userStore.get().findRolesForUser(user); }
@Override public void changePassword(User user, String currentPassword, String newPassword) throws WrongPasswordException, PasswordDoesNotMeetRequirementsException { Preconditions.checkNotNull(user); Preconditions.checkNotNull(user.getId()); User stored = this.userStore.get().findById(user.getId()); if (!this.passwordManager.verifyPassword(currentPassword, stored.getPassword())) { throw new WrongPasswordException("Refusing to change password : given current password is incorrect"); } if (!passwordStrengthChecker.checkLength(newPassword)) { throw new PasswordDoesNotMeetRequirementsException( "Provided password does not meet requirements : length too short"); } if (!passwordStrengthChecker.checkEntropy(newPassword)) { throw new PasswordDoesNotMeetRequirementsException( "Provided password does not meet requirements : not enough bits of entropy"); } this.userStore.get().updatePassword(user, passwordManager.hashPassword(newPassword)); }
@Override public User validateAccount(String validationKey) throws NoSuchValidationKeyException, IncompatibleConnectedUserException, UserAlreadyValidatedException { User user = this.userStore.get().findByValidationKey(validationKey); if (user == null) { throw new NoSuchValidationKeyException("Validation key does not exist"); } if (context.getUser() != null && !context.getUser().getId().equals(user.getId())) { throw new IncompatibleConnectedUserException("Refusing to validate user not matching connected user"); } if (user.isActive()) { throw new UserAlreadyValidatedException("User already validated"); } user.setActive(true); try { this.userStore.get().update(user); } catch (InvalidEntityException | EntityDoesNotExistException e) { this.logger.error("Failed to validate user", e); throw new RuntimeException(e); } return user; }
@Override public Tenant findTenantByDefaultHost(String host) { return this.tenantStore.get().findByDefaultHost(host); }
@Override public List<Tenant> findAllTenants(Integer limit, Integer offset) { return this.tenantStore.get().findAll(limit, offset); }
@Override public boolean hasUsers() { return this.userStore.get().findAll(1, 0).size() > 0; }
@Override public void createTenant(@Valid Tenant tenant) throws EntityAlreadyExistsException, InvalidEntityException { this.tenantStore.get().create(tenant); }
@Override public void updateTenant(@Valid Tenant tenant) throws EntityDoesNotExistException, InvalidEntityException { this.tenantStore.get().update(tenant); }
public void updateSettings(Map<String, Serializable> data) { ValidConfigurationEnforcer enforcer = new ValidConfigurationEnforcer(getExposedPlatformSettingsAsJson(), data); ValidConfigurationEnforcer.ValidationResult result = enforcer.enforce(); TenantConfiguration configuration = new TenantConfiguration(TenantConfiguration.CURRENT_VERSION, result.getResult()); this.tenantStore.get().updateConfiguration(configuration); // Invalidates the cached configuration for the tenant updating its configuration // TODO: do this from the configuration store instead this.configurations.invalidate(this.context.getTenant().getId()); // TODO throw an exception here when there are validation errors, so that it can be acknowledged to the // REST accounts consumer ? (meaning the operation has been partially successful only) }
@Override public void generatePdfInvoice(Order order, OutputStream outputStream) throws InvoicingException { Map<String, Object> invoiceContext = prepareInvoiceContext(order); Tenant tenant = tenantStore.get().findById(order.getTenantId()); Optional<Path> invoicesFolderPath = getInvoicingDirectory(tenant); if (!invoicesFolderPath.isPresent()) { throw new InvoicingException("Cannot generate invoice : no invoice template found"); } try { pdfTemplateRenderer.generatePDF( outputStream, invoicesFolderPath.get().resolve(INVOICE_TEMPLATE_FILE_NAME), invoicesFolderPath.get(), invoiceContext ); } catch (PdfRenderingException e) { throw new InvoicingException("Failed to generate PDF invoice", e); } }
@Override public Optional<User> verify(String value, Tenant tenant) { final int space = value.indexOf(' '); if (space > 0) { final String decoded = B64Code.decode(value.substring(space + 1), StringUtil.__ISO_8859_1); final int i = decoded.indexOf(':'); if (i > 0) { final String username = decoded.substring(0, i); final String password = decoded.substring(i + 1); User user = userStore.get().findUserByEmailOrUserName(username); if (user != null) { if (this.passwordManager.verifyPassword(password, user.getPassword())) { return Optional.of(user); } } } } return Optional.absent(); } }
@Override public boolean userHasRole(User user, Role role) { List<Role> roles = userStore.get().findRolesForUser(user); for (Role userRole : roles) { if (role.equals(userRole)) { return true; } } return false; } }
User user = userStore.get().findUserByEmailOrUserName(username); if (user != null) { if (this.passwordManager.verifyPassword(password, user.getPassword())) {