@@ -38,20 +38,20 @@ public class SystemAdminB2CAccountService {
38
38
private final AzureUserService azureUserService ;
39
39
private final UserRepository userRepository ;
40
40
private final PublicationService publicationService ;
41
- private final AzureAccountService azureAccountService ;
41
+ private final AccountService accountService ;
42
42
private final Integer maxSystemAdminValue ;
43
43
44
44
@ Autowired
45
45
public SystemAdminB2CAccountService (Validator validator , AzureUserService azureUserService ,
46
46
UserRepository userRepository , PublicationService publicationService ,
47
47
@ Value ("${admin.max-system-admin}" )Integer maxSystemAdminValue ,
48
- AzureAccountService azureAccountService ) {
48
+ AccountService accountService ) {
49
49
this .validator = validator ;
50
50
this .azureUserService = azureUserService ;
51
51
this .userRepository = userRepository ;
52
52
this .publicationService = publicationService ;
53
53
this .maxSystemAdminValue = maxSystemAdminValue ;
54
- this .azureAccountService = azureAccountService ;
54
+ this .accountService = accountService ;
55
55
}
56
56
57
57
/**
@@ -61,18 +61,12 @@ public SystemAdminB2CAccountService(Validator validator, AzureUserService azureU
61
61
* @return The PiUser of the created system admin account.
62
62
*/
63
63
public PiUser addSystemAdminAccount (SystemAdminAccount account , String issuerId ) {
64
-
65
- String displayName = "" ;
66
- String provenanceUserId = verifyAdminUser (issuerId );
67
- if (!provenanceUserId .isEmpty ()) {
68
- displayName = azureAccountService .retrieveAzureAccount (provenanceUserId ).getDisplayName ();
69
- }
70
-
71
- validateSystemAdminAccount (account , issuerId , displayName );
64
+ PiUser piUser = accountService .getUserById (UUID .fromString (issuerId ));
65
+ validateSystemAdminAccount (account , issuerId , piUser .getEmail ());
72
66
try {
73
67
User user = azureUserService .createUser (account .convertToAzureAccount (), false );
74
68
PiUser createdUser = userRepository .save (account .convertToPiUser (user .getId ()));
75
- handleNewSystemAdminAccountAction (account , issuerId , ActionResult .SUCCEEDED , displayName );
69
+ handleNewSystemAdminAccountAction (account , issuerId , ActionResult .SUCCEEDED , piUser . getEmail () );
76
70
77
71
publicationService .sendNotificationEmail (
78
72
account .getEmail (),
@@ -83,19 +77,20 @@ public PiUser addSystemAdminAccount(SystemAdminAccount account, String issuerId)
83
77
} catch (AzureCustomException e ) {
84
78
ErroredSystemAdminAccount erroredSystemAdminAccount = new ErroredSystemAdminAccount (account );
85
79
erroredSystemAdminAccount .setErrorMessages (List .of (e .getLocalizedMessage ()));
86
- handleNewSystemAdminAccountAction (account , issuerId , ActionResult .FAILED , displayName );
80
+ handleNewSystemAdminAccountAction (account , issuerId , ActionResult .FAILED , piUser . getEmail () );
87
81
throw new SystemAdminAccountException (erroredSystemAdminAccount );
88
82
}
83
+
89
84
}
90
85
91
86
/**
92
87
* This method handles the logging and publishing that a new system admin account has been created.
93
88
* @param systemAdminAccount The system admin account that has been created
94
89
* @param adminId The ID of the admin user who is creating the account.
95
- * @param name The name of the admin user who is creating the account
90
+ * @param email The email of the admin user who is creating the account
96
91
*/
97
92
public void handleNewSystemAdminAccountAction (SystemAdminAccount systemAdminAccount , String adminId ,
98
- ActionResult result , String name ) {
93
+ ActionResult result , String email ) {
99
94
log .info (writeLog (UUID .fromString (adminId ),
100
95
"has attempted to create a System Admin account, which has: " + result .toString ()));
101
96
@@ -105,7 +100,7 @@ public void handleNewSystemAdminAccountAction(SystemAdminAccount systemAdminAcco
105
100
CreateSystemAdminAction createSystemAdminAction = new CreateSystemAdminAction ();
106
101
createSystemAdminAction .setAccountEmail (systemAdminAccount .getEmail ());
107
102
createSystemAdminAction .setEmailList (existingAdminEmails );
108
- createSystemAdminAction .setRequesterName ( name );
103
+ createSystemAdminAction .setRequesterEmail ( email );
109
104
createSystemAdminAction .setActionResult (result );
110
105
111
106
publicationService .sendSystemAdminAccountAction (createSystemAdminAction );
@@ -115,9 +110,9 @@ public void handleNewSystemAdminAccountAction(SystemAdminAccount systemAdminAcco
115
110
* A helper method which specifically handles validation failures on the system admin account.
116
111
* @param account The system admin account to validate.
117
112
* @param issuerId The ID of the admin user that is issuing the account.
118
- * @param name The name of the admin user requesting the account.
113
+ * @param email The email of the admin user requesting the account.
119
114
*/
120
- private void validateSystemAdminAccount (SystemAdminAccount account , String issuerId , String name ) {
115
+ private void validateSystemAdminAccount (SystemAdminAccount account , String issuerId , String email ) {
121
116
Set <ConstraintViolation <SystemAdminAccount >> constraintViolationSet = validator .validate (account );
122
117
123
118
if (!constraintViolationSet .isEmpty ()) {
@@ -126,14 +121,14 @@ private void validateSystemAdminAccount(SystemAdminAccount account, String issue
126
121
.stream ().map (constraint -> constraint .getPropertyPath ()
127
122
+ ": " + constraint .getMessage ()).toList ());
128
123
129
- handleNewSystemAdminAccountAction (account , issuerId , ActionResult .FAILED , name );
124
+ handleNewSystemAdminAccountAction (account , issuerId , ActionResult .FAILED , email );
130
125
throw new SystemAdminAccountException (erroredSystemAdminAccount );
131
126
}
132
127
133
128
if (userRepository .findByEmailAndUserProvenance (account .getEmail (), UserProvenances .PI_AAD ).isPresent ()) {
134
129
ErroredSystemAdminAccount erroredSystemAdminAccount = new ErroredSystemAdminAccount (account );
135
130
erroredSystemAdminAccount .setDuplicate (true );
136
- handleNewSystemAdminAccountAction (account , issuerId , ActionResult .FAILED , name );
131
+ handleNewSystemAdminAccountAction (account , issuerId , ActionResult .FAILED , email );
137
132
throw new SystemAdminAccountException (erroredSystemAdminAccount );
138
133
}
139
134
@@ -144,22 +139,8 @@ private void validateSystemAdminAccount(SystemAdminAccount account, String issue
144
139
if (systemAdminUsers .size () >= maxSystemAdminValue ) {
145
140
ErroredSystemAdminAccount erroredSystemAdminAccount = new ErroredSystemAdminAccount (account );
146
141
erroredSystemAdminAccount .setAboveMaxSystemAdmin (true );
147
- handleNewSystemAdminAccountAction (account , issuerId , ActionResult .ATTEMPTED , name );
142
+ handleNewSystemAdminAccountAction (account , issuerId , ActionResult .ATTEMPTED , email );
148
143
throw new SystemAdminAccountException (erroredSystemAdminAccount );
149
144
}
150
145
}
151
-
152
- /**
153
- * Method to find whether user is SYSTEM_ADMIN or not.
154
- * @param issuerId The ID of the admin user
155
- * @return Boolean user is SYSTEM_ADMIN or not
156
- */
157
- private String verifyAdminUser (String issuerId ) {
158
- Optional <PiUser > adminUser = userRepository .findByUserId (UUID .fromString (issuerId ));
159
- if (adminUser .isPresent () && adminUser .get ().getRoles ().equals (Roles .SYSTEM_ADMIN )) {
160
- return adminUser .get ().getProvenanceUserId ();
161
- }
162
-
163
- return "" ;
164
- }
165
146
}
0 commit comments