Skip to content

Commit

Permalink
Anomaly RM#85420: Check unicity constraints when creating user at the… (
Browse files Browse the repository at this point in the history
#13607)

* Anomaly RM#85420: Check unicity constraints when creating user at the end of employee creation process

(cherry picked from commit 2222980)
  • Loading branch information
bsu-axelor authored and ale-axelor committed Nov 28, 2024
1 parent 0e744ae commit 79dc5bf
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ private BaseExceptionMessage() {}
"Password must have at least 8 characters with at least three of these four types: lowercase, uppercase, digit, special." /*)*/;
public static final String USER_PATTERN_MISMATCH_CUSTOM = /*$$(*/
"Password doesn't match with configured pattern." /*)*/;
public static final String USER_CODE_LENGTH_SHOULD_BE_GREATER_THAN_2 = /*$$(*/
"Code length should be greater than 2." /*)*/;

/** Convert demo data file */
public static final String DUPLICATE_CSV_FILE_NAME_EXISTS = /*$$(*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.axelor.auth.AuthUtils;
import com.axelor.auth.db.User;
import com.axelor.auth.db.repo.UserRepository;
import com.axelor.common.StringUtils;
import com.axelor.i18n.I18n;
import com.axelor.inject.Beans;
import com.axelor.rpc.ActionRequest;
Expand All @@ -45,7 +46,7 @@
@Singleton
public class UserController {
protected static final Map<String, String> UNIQUE_MESSAGES =
ImmutableMap.of("code", BaseExceptionMessage.USER_CODE_ALREADY_EXISTS);
ImmutableMap.of("code", I18n.get(BaseExceptionMessage.USER_CODE_ALREADY_EXISTS));

public void setUserPartner(ActionRequest request, ActionResponse response) {
try {
Expand Down Expand Up @@ -138,4 +139,21 @@ public void setActiveCompany(ActionRequest request, ActionResponse response) {
TraceBackService.trace(response, e);
}
}

public void validateCode(ActionRequest request, ActionResponse response) {
try {
User user = request.getContext().asType(User.class);
Map<String, String> errors = ModelHelper.getUniqueErrors(user, UNIQUE_MESSAGES);

String code = user.getCode();
if (StringUtils.isEmpty(code) || code.length() < 2) {
errors.put(
"code", I18n.get(BaseExceptionMessage.USER_CODE_LENGTH_SHOULD_BE_GREATER_THAN_2));
}
response.setErrors(errors);
response.setAttr("okBtn", "readonly", !errors.isEmpty());
} catch (Exception e) {
TraceBackService.trace(response, e);
}
}
}
18 changes: 7 additions & 11 deletions axelor-base/src/main/resources/views/User.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
colSpan="12">
<field name="name" placeholder="user name" onChange="action-condition-user-validName"
validIf="name.length >= 2"/>
<field name="code" placeholder="login name" onChange="action-condition-user-validCode"
validIf="code.length >= 2"/>
<field name="code" placeholder="login name"
onChange="action-user-method-validate-code" validIf="code.length >= 2"/>
<field name="group" placeholder="user group"/>
<field name="email" widget="Email"
showIf="!$readonly() || ($readonly() &amp;&amp; email != null)"/>
Expand Down Expand Up @@ -161,8 +161,8 @@
<panel name="overviewPanel" title="Overview" colSpan="12">
<field name="name" placeholder="user name" onChange="action-condition-user-validName"
validIf="name.length >= 2"/>
<field name="code" placeholder="login name" onChange="action-condition-user-validCode"
validIf="code.length >= 2"/>
<field name="code" placeholder="login name"
onChange="action-user-method-validate-code" validIf="code.length >= 2"/>
<field name="group" placeholder="user group"/>
<field name="email" widget="Email"
showIf="!$readonly() || ($readonly() &amp;&amp; email != null)"/>
Expand Down Expand Up @@ -495,13 +495,9 @@
<call class="com.axelor.apps.base.web.UserController" method="validatePassword"/>
</action-method>

<action-condition name="action-condition-user-validCode"
id="base-action-condition-user-validCode">
<check error="Code length should be greater than 2." field="code"
if="code?.length() &lt; 2"/>
<check error="A user with this login already exists." field="code"
if="code &amp;&amp; ![null, id].contains(__repo__(User).findByCode(code)?.id)"/>
</action-condition>
<action-method name="action-user-method-validate-code">
<call class="com.axelor.apps.base.web.UserController" method="validateCode"/>
</action-method>

<action-view name="action-user-view-show-all-permissions" title="All permissions"
model="com.axelor.auth.db.Permission">
Expand Down
5 changes: 3 additions & 2 deletions axelor-human-resource/src/main/resources/views/User.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
onSave="action-employee-view-create-user" onNew="action-employee-record-user-creation-onnew">
<panel name="userPanel">
<field name="group" placeholder="user group"/>
<field name="code" placeholder="login name" onChange="action-condition-user-validCode"
<field name="code" placeholder="login name" onChange="action-user-method-validate-code"
validIf="code.length >= 2" readonlyIf="id &amp;&amp; code == 'admin'" title="Login"/>
<field name="activateOn"
validIf="!activateOn || (activateOn &amp;&amp; !expiresOn) || (activateOn &amp;&amp; expiresOn &amp;&amp; $moment(expiresOn) >= $moment(activateOn))"/>
<field name="expiresOn"
validIf="!expiresOn || (!activateOn &amp;&amp; expiresOn) || (activateOn &amp;&amp; expiresOn &amp;&amp; $moment(expiresOn) >= $moment(activateOn))"/>
<field name="$generateRandomPassword" hidden="true"/>
<button name="okBtn" title="OK" onClick="validate,action-employee-view-create-user"/>
<button name="okBtn" title="OK" readonly="true"
onClick="validate,action-employee-view-create-user"/>
</panel>
</form>

Expand Down
5 changes: 5 additions & 0 deletions changelogs/unreleased/85420.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: "Employee: checked unicity constraints when creating user at the end of employee creation process."
module: axelor-human-resource
developer: |
Renamed `action-condition-user-validCode` to `action-user-method-validate-code`.

0 comments on commit 79dc5bf

Please sign in to comment.