PageRenderTime 59ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/jira-project/jira-components/jira-api/src/main/java/com/atlassian/jira/bc/user/UserService.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 1037 lines | 388 code | 101 blank | 548 comment | 13 complexity | 85a96bd914022ac7a73740a1e0f27d96 MD5 | raw file
Possible License(s): Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. package com.atlassian.jira.bc.user;
  2. import com.atlassian.annotations.ExperimentalApi;
  3. import com.atlassian.annotations.Internal;
  4. import com.atlassian.annotations.PublicApi;
  5. import com.atlassian.application.api.ApplicationKey;
  6. import com.atlassian.crowd.embedded.api.Group;
  7. import com.atlassian.jira.bc.ServiceResultImpl;
  8. import com.atlassian.jira.event.user.UserEventType;
  9. import com.atlassian.jira.exception.AddException;
  10. import com.atlassian.jira.exception.CreateException;
  11. import com.atlassian.jira.exception.PermissionException;
  12. import com.atlassian.jira.exception.RemoveException;
  13. import com.atlassian.jira.plugin.user.WebErrorMessage;
  14. import com.atlassian.jira.user.ApplicationUser;
  15. import com.atlassian.jira.util.ErrorCollection;
  16. import com.atlassian.jira.util.SimpleErrorCollection;
  17. import com.atlassian.jira.util.WarningCollection;
  18. import com.google.common.collect.ImmutableList;
  19. import com.google.common.collect.ImmutableSet;
  20. import javax.annotation.Nonnull;
  21. import javax.annotation.Nullable;
  22. import java.util.Collections;
  23. import java.util.List;
  24. import java.util.Set;
  25. /**
  26. * UserService provides User manipulation methods exposed for remote API and actions.
  27. *
  28. * @since v4.0
  29. */
  30. @PublicApi
  31. public interface UserService {
  32. /**
  33. * Validate that a new user can be created.
  34. * The validation instructions are specified in the {@link CreateUserRequest}.
  35. * <p>
  36. * Typical validations are:
  37. * <ul>
  38. * <li>Validate that specified (or default) application(s) has a seat available for the new user and that the default group(s) has been configured.</li>
  39. * <li>Validate that the specified logged in user is allowed to create a new user.</li>
  40. * <li>Validate that the specified or default user directory exists and is writable.</li>
  41. * <li>Validate that password conforms to password policy.</li>
  42. * <li>Validate that confirm password matches password (optional).</li>
  43. * <li>Validate that the new username is not currently in use.</li>
  44. * </ul>
  45. * </p>
  46. * {@link #createUser(CreateUserValidationResult)} should only be called when the validationResult.isValid().
  47. * <p>
  48. * Usage example:
  49. * <pre>
  50. * <code>final CreateUserRequest createUserRequest = CreateUserRequest.withUserDetails(currentApplicationUser,
  51. * username, password, email, displayName)
  52. * result = userService.validateCreateUser(createUserRequest);
  53. * if(result.isValid())
  54. * {
  55. * userService.createUser(createUserRequest);
  56. * }
  57. * </code>
  58. * </pre>
  59. * This request indicates that the user should be created in the default user directory with access to the default
  60. * applications.
  61. * </p>
  62. *
  63. * @param createUserRequest user creation request containing new user details and validation instructions.
  64. * @return the result that would contain error messages when validation was not successful.
  65. */
  66. CreateUserValidationResult validateCreateUser(CreateUserRequest createUserRequest);
  67. /**
  68. * Create a new application user if the provided {@link CreateUserValidationResult#isValid()}. This is typically
  69. * called after performing {@link #validateCreateUser(CreateUserRequest)} and verifying that the {@link
  70. * CreateUserValidationResult#isValid()}. If the request is not valid or {@link
  71. * #validateCreateUser(CreateUserRequest)} was not called prior to this method an {@link IllegalStateException}
  72. * would get thrown.
  73. *
  74. * @param createUserValidationResult create user validation result returned from {@link
  75. * #validateCreateUser(CreateUserRequest)}, this contains the user details and instructions used to create new
  76. * user.
  77. * @return an application user representing the newly created user.
  78. * @throws PermissionException when the logged in user does not have permissions to perform user creation or when
  79. * the user directory is read-only.
  80. * @throws CreateException if the user could not be created for any reason, eg username already exists
  81. * @see #validateCreateUser(CreateUserRequest)
  82. */
  83. ApplicationUser createUser(CreateUserValidationResult createUserValidationResult)
  84. throws PermissionException, CreateException;
  85. /**
  86. * Allows you to construct {@link ApplicationUser} for {@link #validateUpdateUser(ApplicationUser)}
  87. */
  88. @Nonnull
  89. @ExperimentalApi
  90. ApplicationUserBuilder newUserBuilder(@Nonnull ApplicationUser user);
  91. /**
  92. * Validates creating a user during public signup.
  93. * This method checks that there is a writable User Directory available.
  94. * It also validates that all parameters (username, email, fullname, password) have been provided.
  95. * Email is also checked to ensure that it is a valid email address. The username is required to be
  96. * lowercase characters only and unique. The confirmPassword has to match the password provided.
  97. * <p>
  98. * This validation differs from the 'ForAdminPasswordRequired' and 'ForAdmin' validations as follows: <ul> <li>Does
  99. * not require global admin rights</li> <li>The password is required</li> <ul>
  100. *
  101. * @param loggedInUser The remote user trying to add a new user
  102. * @param username The username of the new user. Needs to be lowercase and unique.
  103. * @param password The password for the new user.
  104. * @param confirmPassword The password confirmation. Needs to match password.
  105. * @param email The email for the new user. Needs to be a valid email address.
  106. * @param fullname The full name for the new user
  107. * @return a validation result containing appropriate errors or the new user's details
  108. * @since 5.0
  109. * @deprecated Use {@link #validateCreateUser(CreateUserRequest)} instead. Since v7.0.
  110. */
  111. CreateUserValidationResult validateCreateUserForSignup(ApplicationUser loggedInUser, String username, String password,
  112. String confirmPassword, String email, String fullname);
  113. /**
  114. * Validates creating a user during setup of JIRA.
  115. * This method does not check for a writable User Directory because the embedded crowd subsystem will not be
  116. * initialised, and we know we will just have an Internal Directory during Setup.
  117. * It also validates that all parameters (username, email, fullname, password) have been
  118. * provided. Email is also checked to ensure that it is a valid email address. The username is required to be
  119. * lowercase characters only and unique. The confirmPassword has to match the password provided.
  120. * <p>
  121. * This validation differs from the 'ForAdminPasswordRequired' and 'ForAdmin' validations as follows: <ul> <li>Does
  122. * not require global admin rights</li> <li>The password is required</li> <ul>
  123. *
  124. * @param loggedInUser The remote user trying to add a new user
  125. * @param username The username of the new user. Needs to be lowercase and unique.
  126. * @param password The password for the new user.
  127. * @param confirmPassword The password confirmation. Needs to match password.
  128. * @param email The email for the new user. Needs to be a valid email address.
  129. * @param fullname The full name for the new user
  130. * @return a validation result containing appropriate errors or the new user's details
  131. * @since 5.0
  132. * @deprecated Use {@link #validateCreateUser(CreateUserRequest)} instead. Since v7.0.
  133. */
  134. CreateUserValidationResult validateCreateUserForSetup(ApplicationUser loggedInUser, String username, String password,
  135. String confirmPassword, String email, String fullname);
  136. /**
  137. * Validates creating a user during setup of JIRA or during public signup.
  138. * This method checks that there is a writable User Directory available.
  139. * It also validates that all parameters (username, email, fullname, password) have been
  140. * provided. Email is also checked to ensure that it is a valid email address. The username is required to be
  141. * lowercase characters only and unique. The confirmPassword has to match the password provided.
  142. * <p>
  143. * This validation differs from the 'ForAdminPasswordRequired' and 'ForAdmin' validations as follows: <ul> <li>Does
  144. * not require global admin rights</li> <li>The password is required</li> <ul>
  145. *
  146. * @param user The remote user trying to add a new user
  147. * @param username The username of the new user. Needs to be lowercase and unique.
  148. * @param password The password for the new user.
  149. * @param confirmPassword The password confirmation. Needs to match password.
  150. * @param email The email for the new user. Needs to be a valid email address.
  151. * @param fullname The full name for the new user
  152. * @return a validation result containing appropriate errors or the new user's details
  153. * @since 4.0
  154. * @deprecated Use {@link #validateCreateUser(CreateUserRequest)} instead. Since v7.0.
  155. */
  156. CreateUserValidationResult validateCreateUserForSignupOrSetup(ApplicationUser user, String username, String password,
  157. String confirmPassword, String email, String fullname);
  158. /**
  159. * Validates creating a user for the admin section. This method checks that external user management is disabled
  160. * and that the user performing the operation has global admin rights. It also validates that all parameters
  161. * (username, email, fullname) except for the password have been provided. Email is also checked to ensure that it
  162. * is a valid email address. The username is required to be lowercase characters only and unique. The
  163. * confirmPassword has to match the password provided.
  164. * <p>
  165. * This validation differs from the 'ForSetup' and 'ForAdminPasswordRequired' validations as follows: <ul> <li>Does
  166. * require global admin rights</li> <li>The password is NOT required</li> </ul>
  167. *
  168. * @param loggedInUser The remote user trying to add a new user
  169. * @param username The username of the new user. Needs to be lowercase and unique.
  170. * @param password The password for the new user.
  171. * @param confirmPassword The password confirmation. Needs to match password.
  172. * @param email The email for the new user. Needs to be a valid email address.
  173. * @param fullname The full name for the new user
  174. * @return a validation result containing appropriate errors or the new user's details
  175. * @since 4.3
  176. * @deprecated Use {@link #validateCreateUser(CreateUserRequest)} instead. Since v7.0.
  177. */
  178. CreateUserValidationResult validateCreateUserForAdmin(ApplicationUser loggedInUser, String username, String password, String confirmPassword,
  179. String email, String fullname);
  180. /**
  181. * Validates creating a user for the admin section. This method checks that external user management is disabled
  182. * and that the user performing the operation has global admin rights. It also validates that all parameters
  183. * (username, email, fullname) except for the password have been provided. Email is also checked to ensure that it
  184. * is a valid email address. The username is required to be lowercase characters only and unique. The
  185. * confirmPassword has to match the password provided.
  186. * <p>
  187. * This method allows the caller to name a directory to create the user in and the directoryId must be valid and
  188. * represent a Directory with "create user" permission.
  189. * <p>
  190. * This validation differs from the 'ForSetup' and 'ForAdminPasswordRequired' validations as follows: <ul> <li>Does
  191. * require global admin rights</li> <li>The password is NOT required</li> </ul>
  192. *
  193. * @param loggedInUser The remote user trying to add a new user
  194. * @param username The username of the new user. Needs to be lowercase and unique.
  195. * @param password The password for the new user.
  196. * @param confirmPassword The password confirmation. Needs to match password.
  197. * @param email The email for the new user. Needs to be a valid email address.
  198. * @param fullname The full name for the new user
  199. * @param directoryId The User Directory
  200. * @return a validation result containing appropriate errors or the new user's details
  201. * @since 4.3.2
  202. * @deprecated Use {@link #validateCreateUser(CreateUserRequest)} instead. Since v7.0.
  203. */
  204. CreateUserValidationResult validateCreateUserForAdmin(ApplicationUser loggedInUser, String username, String password, String confirmPassword,
  205. String email, String fullname, @Nullable Long directoryId);
  206. /**
  207. * Validates the username for a new user. The username is required to be lowercase characters only and unique across
  208. * all directories.
  209. *
  210. * @param loggedInUser The remote user trying to add a new user
  211. * @param username The username of the new user. Needs to be lowercase and unique.
  212. * @return a validation result containing appropriate errors
  213. * @since 5.0.4
  214. */
  215. CreateUsernameValidationResult validateCreateUsername(ApplicationUser loggedInUser, String username);
  216. /**
  217. * Validates the username for a new user. The username is required to be lowercase characters only and unique in the
  218. * given directory.
  219. *
  220. * @param loggedInUser The remote user trying to add a new user
  221. * @param username The username of the new user. Needs to be lowercase and unique.
  222. * @param directoryId The directory which the new user is intended to be created in.
  223. * @return a validation result containing appropriate errors
  224. * @since 5.0.4
  225. */
  226. CreateUsernameValidationResult validateCreateUsername(ApplicationUser loggedInUser, String username, Long directoryId);
  227. /**
  228. * Given a valid validation result, this will create the user using the details provided in the validation result.
  229. * Email notification will be send to created user - via UserEventType.USER_SIGNUP event.
  230. *
  231. * @param result The validation result
  232. * @return The new user object that was created
  233. * @throws CreateException if the user could not be created, eg username already exists
  234. * @throws PermissionException If you cannot create users in this directory (it is read-only).
  235. * @since 4.3
  236. * @deprecated Use {@link #createUser(CreateUserValidationResult)} instead. Since v7.0.
  237. */
  238. ApplicationUser createUserFromSignup(final CreateUserValidationResult result)
  239. throws PermissionException, CreateException;
  240. /**
  241. * Given a valid validation result, this will create the user using the details provided in the validation result.
  242. * Email notification will be send to created user - via UserEventType.USER_CREATED event.
  243. *
  244. * @param result The validation result
  245. * @return The new user object that was created
  246. * @throws CreateException if the user could not be created, eg username already exists
  247. * @throws PermissionException If you cannot create users in this directory (it is read-only).
  248. * @since 4.3
  249. * @deprecated Use {@link #createUser(CreateUserValidationResult)} instead. Since v7.0.
  250. */
  251. ApplicationUser createUserWithNotification(CreateUserValidationResult result)
  252. throws PermissionException, CreateException;
  253. /**
  254. * Given a valid validation result, this will create the user using the details provided in the validation result.
  255. * No email notification will be send to created user.
  256. *
  257. * @param result The validation result
  258. * @return The new user object that was created
  259. * @throws CreateException if the user could not be created, eg username already exists
  260. * @throws PermissionException If you cannot create users in this directory (it is read-only).
  261. * @since 4.3
  262. * @deprecated Use {@link #createUser(CreateUserValidationResult)} instead. Since v7.0.
  263. */
  264. ApplicationUser createUserNoNotification(CreateUserValidationResult result)
  265. throws PermissionException, CreateException;
  266. /**
  267. * Validates updating a user's details.
  268. *
  269. * @param user The user details to update
  270. * @return A validation result containing any errors and all user details
  271. */
  272. UpdateUserValidationResult validateUpdateUser(ApplicationUser user);
  273. /**
  274. * Execute the update using the validation result from {@link #validateUpdateUser(ApplicationUser)}.
  275. *
  276. * @param updateUserValidationResult Result from the validation, which also contains all the user's details.
  277. * @throws IllegalStateException if the validation result contains any errors.
  278. */
  279. void updateUser(UpdateUserValidationResult updateUserValidationResult);
  280. /**
  281. * Validates removing a user for the admin section. This method checks that external user management is disabled
  282. * and that the user performing the operation has global admin rights. It also validates that username have been
  283. * provided.
  284. * <p>
  285. * See {@link #validateDeleteUser(ApplicationUser, ApplicationUser)} for restrictions.
  286. *
  287. * @param loggedInUser The remote user trying to remove a user
  288. * @param username The username of the user to remove. Needs to be valid
  289. * @return a validation result containing appropriate errors or the user object for delete
  290. * @since 6.0
  291. */
  292. DeleteUserValidationResult validateDeleteUser(final ApplicationUser loggedInUser, final String username);
  293. /**
  294. * Validates removing a user for the admin section. This method checks that external user management is disabled
  295. * and that the user performing the operation has global admin rights. It also validates that username have been
  296. * provided.
  297. * <p>
  298. * Removing the user is not allowed if:
  299. * <ul>
  300. * <li>Logged in user lacks global admin permissions</li>
  301. * <li>Logged in user and target user are the same user</li>
  302. * <li>Logged in user is not a system administrator but the target user is</li>
  303. * <li>Target user is the assignee for any issue</li>
  304. * <li>Target user is the reporter for any issue</li>
  305. * <li>Target user is the project lead for any project</li>
  306. * <li>A plugin that implements {@link com.atlassian.jira.plugin.user.PreDeleteUserErrors} rejects the request</li>
  307. * </ul>
  308. *
  309. * @param loggedInUser The remote user trying to remove a user
  310. * @param userForDelete The user to remove. Needs to be valid
  311. * @return a validation result containing appropriate errors or the user object for delete
  312. * @since 6.0
  313. */
  314. DeleteUserValidationResult validateDeleteUser(final ApplicationUser loggedInUser, final ApplicationUser userForDelete);
  315. /**
  316. * Given a valid validation result, this will remove the user and removes the user from all the groups. All
  317. * components lead by user will have lead cleared.
  318. *
  319. * @param loggedInUser the user performing operation
  320. * @param result The validation result
  321. * @deprecated Use {@link #removeUser(ApplicationUser, DeleteUserValidationResult)} instead. Since v6.0.
  322. */
  323. void removeUser(ApplicationUser loggedInUser, final DeleteUserValidationResult result);
  324. /**
  325. * Validates associating a user with an application. This method checks there is an application specified by the
  326. * application key. It also checks if there are any default groups assigned to that application. It validates if
  327. * there are any spaces in the application license as well. This also validates if the directory where the user
  328. * lives is not fully read-only.
  329. *
  330. * @param user the user to be associated with the application
  331. * @param applicationKey the key of the application
  332. * @return a validation result containing appropriate errors or the details used to associate a user with an application
  333. * @since 7.0
  334. * @deprecated Use {@link #validateAddUserToApplication(ApplicationUser, ApplicationUser, ApplicationKey)} instead. Since v7.0
  335. */
  336. AddUserToApplicationValidationResult validateAddUserToApplication(ApplicationUser user, ApplicationKey applicationKey);
  337. /**
  338. * Validates associating a user with an application. This method checks if the logged in user has the permission to
  339. * add user to application. It then checks if there is an application specified by the application key. It also checks if
  340. * there are any default groups assigned to that application. It validates if there are any spaces in the application
  341. * license as well. This also validates if the directory where the user lives is not fully read-only.
  342. *
  343. * @param loggedInUser The logged in user
  344. * @param user the user to be associated with the application
  345. * @param applicationKey the key of the application
  346. * @return a validation result containing appropriate errors or the details used to associate a user with an application
  347. * @since 7.0
  348. */
  349. AddUserToApplicationValidationResult validateAddUserToApplication(ApplicationUser loggedInUser, ApplicationUser user, ApplicationKey applicationKey);
  350. /**
  351. * Given a valid validation result, this will associate a user with an application by adding that user to all the
  352. * default groups assigned to that application (regardless of the license).
  353. * <p>
  354. * If the user is already associated with the application but is not the member of all the default groups, that user
  355. * will be added to the default groups which that user does not belong to yet.
  356. * <p>
  357. * If any of the default groups assigned to this application are also assigned to other application(s), the user
  358. * will also be associated with those applications as well.
  359. * <p>
  360. * This method is not the exact opposite to {@link com.atlassian.jira.bc.user.UserService#removeUserFromApplication}.
  361. * While this method only adds user to the default groups assigned to an application, removeUserFromApplication
  362. * method removes user from all the group assigned to an application.
  363. *
  364. * @param result the validation result
  365. * @throws com.atlassian.jira.exception.AddException the underlying directory implementation failed to add user to group
  366. * @throws com.atlassian.jira.exception.PermissionException the underlying directory has been configured to not allow
  367. * user to be added to group, or the directory/group is read-only
  368. * @since v7.0
  369. */
  370. void addUserToApplication(AddUserToApplicationValidationResult result)
  371. throws AddException, PermissionException;
  372. /**
  373. * Validates disassociating a user from an application. This method checks there is an application specified by the
  374. * application key.
  375. * <p>
  376. * Method expects for loggedInUser to be available via {@link com.atlassian.jira.security.JiraAuthenticationContext#getLoggedInUser()}.
  377. *
  378. * @param user the user to be disassociated with the application
  379. * @param applicationKey the key of the application
  380. * @return a validation result containing appropriate errors or the details used to disassociate a user from an application
  381. * @since 7.0
  382. * @deprecated Use {@link UserService#validateRemoveUserFromApplication(com.atlassian.jira.user.ApplicationUser, com.atlassian.jira.user.ApplicationUser, com.atlassian.application.api.ApplicationKey)} instead. Since v 7.0.0.
  383. */
  384. @Deprecated
  385. @Internal
  386. RemoveUserFromApplicationValidationResult validateRemoveUserFromApplication(ApplicationUser user, ApplicationKey applicationKey);
  387. /**
  388. * Validates disassociating a user from an application. This method checks there is an application specified by the
  389. * application key.
  390. *
  391. * @param loggedInUser application user performing the application access removal
  392. * @param user the user to be disassociated with the application
  393. * @param applicationKey the key of the application
  394. * @return a validation result containing appropriate errors or the details used to disassociate a user from an application
  395. * @since 7.0
  396. */
  397. RemoveUserFromApplicationValidationResult validateRemoveUserFromApplication(ApplicationUser loggedInUser, ApplicationUser user, ApplicationKey applicationKey);
  398. /**
  399. * Given a valid validation result, this will disassociate a user from an application by removing that user from all
  400. * the groups assigned to that application. This also validates if the directory where the user lives is not fully read-only.
  401. * <p>
  402. * If the group set assigned to this application is a superset of the group set assigned to other application(s),
  403. * the user will also be disassociated with those applications as well.
  404. * <p>
  405. * This method is not the exact opposite to {@link com.atlassian.jira.bc.user.UserService#addUserToApplication}.
  406. * While this method removes user from all the group assigned to an application, addUserToApplication method
  407. * only adds user to the default groups assigned to an application
  408. *
  409. * @param result The validation result
  410. * @throws com.atlassian.jira.exception.RemoveException the underlying directory implementation failed to remove user from group
  411. * @throws com.atlassian.jira.exception.PermissionException the underlying directory has been configured to not allow
  412. * user to be removed from group, or the directory/group is read-only
  413. * @since v7.0
  414. */
  415. void removeUserFromApplication(RemoveUserFromApplicationValidationResult result)
  416. throws RemoveException, PermissionException;
  417. @PublicApi
  418. final class CreateUserValidationResult extends ServiceResultImpl {
  419. private final List<WebErrorMessage> passwordErrors;
  420. private final Set<ApplicationKey> applicationKeys;
  421. private final CreateUserRequest createUserRequest;
  422. CreateUserValidationResult(ErrorCollection errorCollection) {
  423. this(errorCollection, ImmutableList.of());
  424. }
  425. CreateUserValidationResult(ErrorCollection errorCollection, List<WebErrorMessage> passwordErrors) {
  426. super(errorCollection);
  427. this.createUserRequest = null;
  428. this.passwordErrors = ImmutableList.copyOf(passwordErrors);
  429. this.applicationKeys = Collections.emptySet();
  430. }
  431. CreateUserValidationResult(final String username,
  432. final String password, final String email, final String fullname) {
  433. this(username, password, email, fullname, null);
  434. }
  435. CreateUserValidationResult(final String username,
  436. final String password, final String email, final String fullname, Long directoryId) {
  437. this(username, password, email, fullname, directoryId, Collections.emptySet());
  438. }
  439. CreateUserValidationResult(final String username, final String password, final String email,
  440. final String fullname, final Long directoryId, @Nullable final Set<ApplicationKey> applicationKeys) {
  441. super(new SimpleErrorCollection());
  442. this.createUserRequest = CreateUserRequest
  443. .withUserDetails(null, username, password, email, fullname)
  444. .inDirectory(directoryId);
  445. this.passwordErrors = ImmutableList.of();
  446. this.applicationKeys = applicationKeys;
  447. }
  448. CreateUserValidationResult(final CreateUserRequest createUserRequest, final Set<ApplicationKey> applicationKeys,
  449. ErrorCollection errorCollection, List<WebErrorMessage> passwordErrors, final WarningCollection warningCollection) {
  450. super(errorCollection, warningCollection);
  451. this.passwordErrors = passwordErrors;
  452. this.applicationKeys = applicationKeys;
  453. this.createUserRequest = createUserRequest;
  454. }
  455. CreateUserRequest getCreateUserRequest() {
  456. return createUserRequest;
  457. }
  458. boolean hasCreateUserRequest() {
  459. return createUserRequest != null;
  460. }
  461. public String getUsername() {
  462. return createUserRequest == null ? null : createUserRequest.getUsername();
  463. }
  464. public String getPassword() {
  465. return createUserRequest == null ? null : createUserRequest.getPassword();
  466. }
  467. public String getEmail() {
  468. return createUserRequest == null ? null : createUserRequest.getEmailAddress();
  469. }
  470. public String getFullname() {
  471. return createUserRequest == null ? null : createUserRequest.getDisplayName();
  472. }
  473. public Long getDirectoryId() {
  474. return createUserRequest == null ? null : createUserRequest.getDirectoryId();
  475. }
  476. public List<WebErrorMessage> getPasswordErrors() {
  477. return passwordErrors;
  478. }
  479. public Set<ApplicationKey> getApplicationKeys() {
  480. if (applicationKeys == null) {
  481. return ImmutableSet.of();
  482. } else {
  483. return applicationKeys;
  484. }
  485. }
  486. }
  487. @PublicApi
  488. final class CreateUsernameValidationResult extends ServiceResultImpl {
  489. private final String username;
  490. private final Long directoryId;
  491. public CreateUsernameValidationResult(final String username, final Long directoryId, final ErrorCollection errorCollection) {
  492. super(errorCollection);
  493. this.username = username;
  494. this.directoryId = directoryId;
  495. }
  496. public String getUsername() {
  497. return username;
  498. }
  499. public Long getDirectoryId() {
  500. return directoryId;
  501. }
  502. }
  503. @PublicApi
  504. static final class UpdateUserValidationResult extends ServiceResultImpl {
  505. private final ApplicationUser user;
  506. UpdateUserValidationResult(ErrorCollection errorCollection) {
  507. super(errorCollection);
  508. user = null;
  509. }
  510. UpdateUserValidationResult(final ApplicationUser user) {
  511. super(new SimpleErrorCollection());
  512. this.user = user;
  513. }
  514. /**
  515. * @return the user
  516. * @deprecated Use {@link #getApplicationUser()} instead. Since v6.0.
  517. */
  518. public ApplicationUser getUser() {
  519. return user;
  520. }
  521. public ApplicationUser getApplicationUser() {
  522. return user;
  523. }
  524. }
  525. @PublicApi
  526. static final class DeleteUserValidationResult extends ServiceResultImpl {
  527. private final ApplicationUser user;
  528. DeleteUserValidationResult(ErrorCollection errorCollection) {
  529. super(errorCollection);
  530. user = null;
  531. }
  532. DeleteUserValidationResult(final ApplicationUser user) {
  533. super(new SimpleErrorCollection());
  534. this.user = user;
  535. }
  536. public ApplicationUser getUser() {
  537. return user;
  538. }
  539. public ApplicationUser getApplicationUser() {
  540. return user;
  541. }
  542. }
  543. @PublicApi
  544. static final class AddUserToApplicationValidationResult extends ServiceResultImpl {
  545. private final ApplicationUser userToAdd;
  546. private final Set<Group> applicationDefaultGroups;
  547. AddUserToApplicationValidationResult(ErrorCollection errorCollection) {
  548. super(errorCollection);
  549. userToAdd = null;
  550. applicationDefaultGroups = null;
  551. }
  552. AddUserToApplicationValidationResult(final ApplicationUser userToAdd, final Set<Group> applicationDefaultGroups) {
  553. super(new SimpleErrorCollection());
  554. this.userToAdd = userToAdd;
  555. this.applicationDefaultGroups = applicationDefaultGroups;
  556. }
  557. public ApplicationUser getUserToAdd() {
  558. return userToAdd;
  559. }
  560. public Set<Group> getApplicationDefaultGroups() {
  561. return applicationDefaultGroups;
  562. }
  563. }
  564. @PublicApi
  565. static final class RemoveUserFromApplicationValidationResult extends ServiceResultImpl {
  566. private final ApplicationUser userToRemove;
  567. private final Set<String> applicationGroupNames;
  568. RemoveUserFromApplicationValidationResult(ErrorCollection errorCollection) {
  569. super(errorCollection);
  570. userToRemove = null;
  571. applicationGroupNames = null;
  572. }
  573. RemoveUserFromApplicationValidationResult(final ApplicationUser userToRemove, final Set<String> applicationGroupNames) {
  574. super(new SimpleErrorCollection());
  575. this.userToRemove = userToRemove;
  576. this.applicationGroupNames = applicationGroupNames;
  577. }
  578. public ApplicationUser getUserToRemove() {
  579. return userToRemove;
  580. }
  581. public Set<String> getApplicationGroupNames() {
  582. return applicationGroupNames;
  583. }
  584. }
  585. @PublicApi
  586. static final class FieldName {
  587. private FieldName() {
  588. }
  589. /**
  590. * The default name of HTML fields containing a User's email. Validation methods on this service will return an
  591. * {@link com.atlassian.jira.util.ErrorCollection} with error messages keyed to this field name.
  592. */
  593. static String EMAIL = "email";
  594. /**
  595. * The default name of HTML fields containing a User's username. Validation methods on this service will return
  596. * an {@link com.atlassian.jira.util.ErrorCollection} with error messages keyed to this field name.
  597. */
  598. static String NAME = "username";
  599. /**
  600. * The default name of HTML fields containing a User's full name. Validation methods on this service will return
  601. * an {@link com.atlassian.jira.util.ErrorCollection} with error messages keyed to this field name.
  602. */
  603. static String FULLNAME = "fullname";
  604. /**
  605. * The default name of HTML fields containing a User's password. Validation methods on this service will return
  606. * an {@link com.atlassian.jira.util.ErrorCollection} with error messages keyed to this field name.
  607. */
  608. static String PASSWORD = "password";
  609. /**
  610. * The default name of HTML fields containing a User's password confirmation. Validation methods on this service
  611. * will return an {@link com.atlassian.jira.util.ErrorCollection} with error messages keyed to this field name.
  612. */
  613. static String CONFIRM_PASSWORD = "confirm";
  614. }
  615. /**
  616. * This request contains all the instructions and user details that should be used during user validation and user
  617. * creation.
  618. */
  619. @PublicApi
  620. final class CreateUserRequest {
  621. // The logged in user performing the user creation
  622. private final ApplicationUser loggedInUser;
  623. // required user details for new user
  624. private final String username;
  625. // null or empty makes random one to generate
  626. private final String password;
  627. // required user details for new user
  628. private final String displayName;
  629. // required user details for new user
  630. private final String emailAddress;
  631. // null directoryId represents default user directory
  632. private final Long directoryId;
  633. // Validation of confirm password optional, default = null
  634. private final String confirmPassword;
  635. // null indicates Default Application Access, default = null
  636. private final Set<ApplicationKey> applicationKeys;
  637. // Notifications are sent by default, default = true
  638. private final boolean sendNotification;
  639. // Is the password required
  640. private final boolean passwordRequired;
  641. // Should perform logged in user permission check
  642. private final boolean performPermissionCheck;
  643. // Indicates whether all validations should be skipped
  644. private final boolean skipValidation;
  645. private final int userEventType;
  646. private CreateUserRequest(@Nullable final ApplicationUser loggedInUser,
  647. final String username, @Nullable final String password,
  648. final String emailAddress, final String displayName,
  649. @Nullable final Long directoryId,
  650. @Nullable final String confirmPassword, @Nullable Set<ApplicationKey> applicationKeys,
  651. final boolean sendNotification,
  652. final boolean passwordRequired, final boolean performPermissionCheck, final boolean skipValidation,
  653. final int userEventType) {
  654. this.loggedInUser = loggedInUser;
  655. this.username = username;
  656. this.password = password;
  657. this.emailAddress = emailAddress;
  658. this.displayName = displayName;
  659. this.directoryId = directoryId;
  660. this.confirmPassword = confirmPassword;
  661. this.applicationKeys = applicationKeys;
  662. this.sendNotification = sendNotification;
  663. this.passwordRequired = passwordRequired;
  664. this.performPermissionCheck = performPermissionCheck;
  665. this.skipValidation = skipValidation;
  666. this.userEventType = userEventType;
  667. }
  668. /**
  669. * Create a new user creation request as the provided user for the specified user details. This request
  670. * indicates that the new users should be created in the default user directory and have access to the default
  671. * applications. A Notification would be sent after user creation.
  672. *
  673. * @param loggedInUser application user performing the create. If {@code null} the user creation is being
  674. * performed during setup or signup.
  675. * @param username The username of the new user. Needs to be lowercase and unique. Required.
  676. * @param password The password for the new user. If empty a random password will be generated.
  677. * @param emailAddress The email address for the new user. Required.
  678. * @param displayName The display name for the new user. Required.
  679. */
  680. public static CreateUserRequest withUserDetails(@Nullable final ApplicationUser loggedInUser,
  681. final String username, @Nullable final String password, final String emailAddress, final String displayName) {
  682. return new CreateUserRequest(loggedInUser, username, password, emailAddress, displayName, null,
  683. null, null, true, false, true, false, UserEventType.USER_CREATED);
  684. }
  685. /**
  686. * Confirm that the provided password matches the confirm password provided.
  687. *
  688. * @param confirmPassword the confirm password that should match the provided password.
  689. */
  690. public CreateUserRequest confirmPassword(final String confirmPassword) {
  691. return new CreateUserRequest(loggedInUser, username, password, emailAddress, displayName, directoryId,
  692. confirmPassword, applicationKeys, sendNotification, passwordRequired,
  693. performPermissionCheck, skipValidation, userEventType);
  694. }
  695. /**
  696. * Indicate that no application access is required for the new user.
  697. */
  698. public CreateUserRequest withNoApplicationAccess() {
  699. return new CreateUserRequest(loggedInUser, username, password, emailAddress, displayName, directoryId,
  700. confirmPassword, ImmutableSet.of(), sendNotification, passwordRequired,
  701. performPermissionCheck, skipValidation, userEventType);
  702. }
  703. /**
  704. * Specify the applications that this user required access to.
  705. *
  706. * @param applicationKeys applications that user should be granted access to, an empty Set indicates that the
  707. * user does not require access to any applications.
  708. * @see #withNoApplicationAccess()
  709. */
  710. public CreateUserRequest withApplicationAccess(@Nonnull final Set<ApplicationKey> applicationKeys) {
  711. return new CreateUserRequest(loggedInUser, username, password, emailAddress, displayName, directoryId,
  712. confirmPassword, applicationKeys, sendNotification, passwordRequired,
  713. performPermissionCheck, skipValidation, userEventType);
  714. }
  715. /**
  716. * Specify whether a notification should be sent when the new user has successfully been created.
  717. *
  718. * @param sendNotification true if notification should be sent after a new user has successfully been created.
  719. * false if no notification should be sent.
  720. */
  721. public CreateUserRequest sendNotification(final boolean sendNotification) {
  722. return new CreateUserRequest(loggedInUser, username, password, emailAddress, displayName, directoryId,
  723. confirmPassword, applicationKeys, sendNotification, passwordRequired,
  724. performPermissionCheck, skipValidation, userEventType);
  725. }
  726. /**
  727. * Specify that a user signup event should be sent after user creation. Typically when the new user is being
  728. * created during signup.
  729. */
  730. public CreateUserRequest sendUserSignupEvent() {
  731. return new CreateUserRequest(loggedInUser, username, password, emailAddress, displayName, directoryId,
  732. confirmPassword, applicationKeys, sendNotification, passwordRequired,
  733. performPermissionCheck, skipValidation, UserEventType.USER_SIGNUP);
  734. }
  735. /**
  736. * Specify that the user password is required.
  737. */
  738. public CreateUserRequest passwordRequired() {
  739. return new CreateUserRequest(loggedInUser, username, password, emailAddress, displayName, directoryId,
  740. confirmPassword, applicationKeys, sendNotification, true,
  741. performPermissionCheck, skipValidation, userEventType);
  742. }
  743. /**
  744. * Indicate that the permission check should not be performed on the logged in user to determine whether the
  745. * logged in user has permission to create the new user.
  746. *
  747. * @param performPermissionCheck true if the permission check should be performed else false to skip the
  748. * permission check.
  749. */
  750. public CreateUserRequest performPermissionCheck(boolean performPermissionCheck) {
  751. return new CreateUserRequest(loggedInUser, username, password, emailAddress, displayName, directoryId,
  752. confirmPassword, applicationKeys, sendNotification, passwordRequired,
  753. performPermissionCheck, skipValidation, userEventType);
  754. }
  755. /**
  756. * Specify what user directory should be used for new user.
  757. *
  758. * @param directoryId The directory which the new user is intended to be created in. {@code null} indicates that
  759. * the default directory should be used.
  760. */
  761. public CreateUserRequest inDirectory(final Long directoryId) {
  762. return new CreateUserRequest(loggedInUser, username, password, emailAddress, displayName, directoryId,
  763. confirmPassword, applicationKeys, sendNotification, passwordRequired,
  764. performPermissionCheck, skipValidation, userEventType);
  765. }
  766. /**
  767. * Specify that all validations should be skipped. This should only be used when it does not matter that user
  768. * gets created or added.
  769. */
  770. @Internal
  771. public CreateUserRequest skipValidation() {
  772. return new CreateUserRequest(loggedInUser, username, password, emailAddress, displayName, directoryId,
  773. confirmPassword, applicationKeys, sendNotification, passwordRequired,
  774. performPermissionCheck, true, userEventType);
  775. }
  776. /**
  777. * This method has been made public only to allow temporary backwards compatibility and will be removed in 7.0.
  778. * <p>
  779. * Set the event type to be dispatch after user creation.
  780. * This has been added for compatibility reasons please avoid using this.
  781. *
  782. * @param userEventType the event type that should be sent after user has been created.
  783. * @see UserEventType
  784. * @deprecated Since v7.0.
  785. */
  786. @Internal
  787. public CreateUserRequest withEventUserEvent(final int userEventType) {
  788. return new CreateUserRequest(loggedInUser, username, password, emailAddress, displayName, directoryId,
  789. confirmPassword, applicationKeys, sendNotification, passwordRequired,
  790. performPermissionCheck, skipValidation, userEventType);
  791. }
  792. /**
  793. * Determine whether the user password is required.
  794. *
  795. * @return true is user password is required else faslse.
  796. */
  797. public boolean requirePassword() {
  798. return passwordRequired;
  799. }
  800. /**
  801. * Return the user name that is used to identify the new user.
  802. *
  803. * @return the user name that is used to identify the new user.
  804. */
  805. public String getUsername() {
  806. return username;
  807. }
  808. /**
  809. * Return the password for the new user.
  810. *
  811. * @return the password for the new user.
  812. */
  813. public String getPassword() {
  814. return password;
  815. }
  816. /**
  817. * Return the display name (also known as full name) for the new user.
  818. *
  819. * @return the display name (also known as full name) for the new user.
  820. */
  821. public String getDisplayName() {
  822. return displayName;
  823. }
  824. /**
  825. * Return the email address for the new user.
  826. *
  827. * @return the email address for the new user.
  828. */
  829. public String getEmailAddress() {
  830. return emailAddress;
  831. }
  832. /**
  833. * Determine whether the default user directory should be used for the new user.
  834. */
  835. public boolean defaultDirectory() {
  836. return directoryId == null;
  837. }
  838. /**
  839. * Return the directory id (representing the user directory), that the new user should be created in. This is
  840. * typically left unset {@code null} indicating default.
  841. *
  842. * @return the directory id (representing the user directory), that the new user should be created in.
  843. */
  844. public Long getDirectoryId() {
  845. return directoryId;
  846. }
  847. /**
  848. * Determine whether the confirmation password should be validation or whether the confirmation password has
  849. * been set.
  850. *
  851. * @return true if the {@link #confirmPassword(String)} should be compared against the {@link #getPassword()}.
  852. */
  853. public boolean shouldConfirmPassword() {
  854. return confirmPassword != null;
  855. }
  856. /**
  857. * Return the confirmation password that should be compared against the {@link #getPassword()}. This is
  858. * typically set by {@link #confirmPassword(String)} to indicate that the confirm password should be validated
  859. * against the {@link #getPassword()}.
  860. *
  861. * @return the confirmation password that should be compared against the {@link #getPassword()}.
  862. */
  863. public String getConfirmPassword() {
  864. return confirmPassword;
  865. }
  866. /**
  867. * Determine whether the new user requires default application access. Default application access is the
  868. * configuration that indicates what applications a new user should be added to.
  869. *
  870. * @return true if the new user should be granted access to the default applications else false.
  871. */
  872. public boolean requireDefaultApplicationAccess() {
  873. return applicationKeys == null;
  874. }
  875. /**
  876. * Return the application keys that are used to specify what application access the new user requires.
  877. *
  878. * @return the application keys that are used to specify what application access the new user requires.
  879. * @see CreateUserRequest#requireDefaultApplicationAccess()
  880. */
  881. public Set<ApplicationKey> getApplicationKeys() {
  882. return applicationKeys;
  883. }
  884. /**
  885. * Determine whether a notification should be sent after user creation.
  886. *
  887. * @return true if a notification should be sent after user creation. false if a notification should not be sent
  888. * after user creation.
  889. * @see com.atlassian.jira.bc.user.UserService.CreateUserRequest#sendNotification(boolean)
  890. */
  891. public boolean shouldSendNotification() {
  892. return sendNotification;
  893. }
  894. /**
  895. * Determine whether a signup event should be sent after user has been created. This is typically true when the
  896. * user is being created via signup.
  897. *
  898. * @return true if a signup event should be sent after user has been created.
  899. */
  900. public boolean shouldSendUserSignupEvent() {

Large files files are truncated, but you can click here to view the full file