]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/UserIdentification/UserProfileManagerDxe/UserProfileDelete.c
Update the UserProfileManagerDxe to keep the old behaviors after add exit action...
[mirror_edk2.git] / SecurityPkg / UserIdentification / UserProfileManagerDxe / UserProfileDelete.c
1 /** @file
2 The functions to delete a user profile.
3
4 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "UserProfileManager.h"
16
17 /**
18 Get the username from the specified user.
19
20 @param[in] User Handle of a user profile.
21
22 @retval EFI_STRING_ID The String Id of the user's username.
23
24 **/
25 EFI_STRING_ID
26 GetUserName (
27 IN EFI_USER_PROFILE_HANDLE User
28 )
29 {
30 EFI_STATUS Status;
31 EFI_USER_INFO_HANDLE UserInfo;
32 EFI_USER_INFO *Info;
33 UINTN InfoSize;
34 UINTN MemSize;
35 UINTN NameLen;
36 CHAR16 UserName[USER_NAME_LENGTH];
37 EFI_STRING_ID UserId;
38
39 //
40 // Allocate user information memory.
41 //
42 MemSize = sizeof (EFI_USER_INFO) + 63;
43 Info = AllocateZeroPool (MemSize);
44 ASSERT (Info != NULL);
45
46 //
47 // Get user name information.
48 //
49 UserInfo = NULL;
50 while (TRUE) {
51 InfoSize = MemSize;
52 //
53 // Get next user information.
54 //
55 Status = mUserManager->GetNextInfo (
56 mUserManager,
57 User,
58 &UserInfo
59 );
60 if (EFI_ERROR (Status)) {
61 break;
62 }
63
64 Status = mUserManager->GetInfo (
65 mUserManager,
66 User,
67 UserInfo,
68 Info,
69 &InfoSize
70 );
71 if (Status == EFI_BUFFER_TOO_SMALL) {
72 MemSize = InfoSize;
73 FreePool (Info);
74 Info = AllocateZeroPool (MemSize);
75 ASSERT (Info != NULL);
76
77 Status = mUserManager->GetInfo (
78 mUserManager,
79 User,
80 UserInfo,
81 Info,
82 &InfoSize
83 );
84 }
85 //
86 // Check user information.
87 //
88 if (Status == EFI_SUCCESS) {
89 if (Info->InfoType == EFI_USER_INFO_NAME_RECORD) {
90 NameLen = Info->InfoSize - sizeof (EFI_USER_INFO);
91 if (NameLen > USER_NAME_LENGTH * sizeof (CHAR16)) {
92 NameLen = USER_NAME_LENGTH * sizeof (CHAR16);
93 }
94 ASSERT (NameLen >= sizeof (CHAR16));
95 CopyMem (UserName, (UINT8 *) (Info + 1), NameLen);
96 UserName[NameLen / sizeof (CHAR16) - 1] = 0;
97 UserId = HiiSetString (
98 mCallbackInfo->HiiHandle,
99 0,
100 UserName,
101 NULL
102 );
103 if (UserId != 0) {
104 FreePool (Info);
105 return UserId;
106 }
107 }
108 }
109 }
110
111 FreePool (Info);
112 return 0;
113 }
114
115
116 /**
117 Add a username item in form.
118
119 @param[in] User Points to the user profile whose username is added.
120 @param[in] Index The index of the user in the user name list
121 @param[in] OpCodeHandle Points to container for dynamic created opcodes.
122
123 **/
124 VOID
125 AddUserToForm (
126 IN EFI_USER_PROFILE_HANDLE User,
127 IN UINT16 Index,
128 IN VOID *OpCodeHandle
129 )
130 {
131 EFI_STRING_ID NameId;
132
133 //
134 // Get user name
135 //
136 NameId = GetUserName (User);
137 if (NameId == 0) {
138 return ;
139 }
140
141 //
142 // Create user name option.
143 //
144 switch (Index & KEY_FIRST_FORM_MASK) {
145 case KEY_MODIFY_USER:
146 HiiCreateGotoOpCode (
147 OpCodeHandle, // Container for dynamic created opcodes
148 FORMID_USER_INFO, // Target Form ID
149 NameId, // Prompt text
150 STRING_TOKEN (STR_NULL_STRING), // Help text
151 EFI_IFR_FLAG_CALLBACK, // Question flag
152 Index // Question ID
153 );
154 break;
155
156 case KEY_DEL_USER:
157 HiiCreateActionOpCode (
158 OpCodeHandle, // Container for dynamic created opcodes
159 Index, // Question ID
160 NameId, // Prompt text
161 STRING_TOKEN (STR_NULL_STRING), // Help text
162 EFI_IFR_FLAG_CALLBACK, // Question flag
163 0 // Action String ID
164 );
165 break;
166
167 default:
168 break;
169 }
170 }
171
172
173 /**
174 Delete the user specified by UserIndex in user profile database.
175
176 @param[in] UserIndex The index of user in the user name list
177 to be deleted.
178
179 **/
180 VOID
181 DeleteUser (
182 IN UINT8 UserIndex
183 )
184 {
185 EFI_STATUS Status;
186 EFI_USER_PROFILE_HANDLE User;
187 EFI_INPUT_KEY Key;
188
189 //
190 // Find specified user profile and delete it.
191 //
192 User = NULL;
193 Status = mUserManager->GetNext (mUserManager, &User);
194 if (EFI_ERROR (Status)) {
195 goto Done;
196 }
197
198 while (UserIndex > 1) {
199 Status = mUserManager->GetNext (mUserManager, &User);
200 if (EFI_ERROR (Status)) {
201 goto Done;
202 }
203 UserIndex--;
204 }
205
206 if (UserIndex == 1) {
207 Status = mUserManager->Delete (mUserManager, User);
208 if (EFI_ERROR (Status)) {
209 goto Done;
210 }
211 CreatePopUp (
212 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
213 &Key,
214 L"Delete User Succeed!",
215 L"",
216 L"Please Press Any Key to Continue ...",
217 NULL
218 );
219 return ;
220 }
221
222 Done:
223 CreatePopUp (
224 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
225 &Key,
226 L"Delete User Failed!",
227 L"",
228 L"Please Press Any Key to Continue ...",
229 NULL
230 );
231 }
232
233
234 /**
235 Display user select form, cab select a user to delete.
236
237 **/
238 VOID
239 SelectUserToDelete (
240 VOID
241 )
242 {
243 EFI_STATUS Status;
244 UINT8 Index;
245 EFI_USER_PROFILE_HANDLE User;
246 EFI_USER_PROFILE_HANDLE CurrentUser;
247 VOID *StartOpCodeHandle;
248 VOID *EndOpCodeHandle;
249 EFI_IFR_GUID_LABEL *StartLabel;
250 EFI_IFR_GUID_LABEL *EndLabel;
251
252 //
253 // Initialize the container for dynamic opcodes.
254 //
255 StartOpCodeHandle = HiiAllocateOpCodeHandle ();
256 ASSERT (StartOpCodeHandle != NULL);
257
258 EndOpCodeHandle = HiiAllocateOpCodeHandle ();
259 ASSERT (EndOpCodeHandle != NULL);
260
261 //
262 // Create Hii Extend Label OpCode.
263 //
264 StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (
265 StartOpCodeHandle,
266 &gEfiIfrTianoGuid,
267 NULL,
268 sizeof (EFI_IFR_GUID_LABEL)
269 );
270 StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
271 StartLabel->Number = LABEL_USER_DEL_FUNC;
272
273 EndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (
274 EndOpCodeHandle,
275 &gEfiIfrTianoGuid,
276 NULL,
277 sizeof (EFI_IFR_GUID_LABEL)
278 );
279 EndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
280 EndLabel->Number = LABEL_END;
281
282 //
283 // Add each user can be deleted.
284 //
285 User = NULL;
286 Index = 1;
287 mUserManager->Current (mUserManager, &CurrentUser);
288 while (TRUE) {
289 Status = mUserManager->GetNext (mUserManager, &User);
290 if (EFI_ERROR (Status)) {
291 break;
292 }
293
294 if (User != CurrentUser) {
295 AddUserToForm (
296 User,
297 (UINT16)(KEY_DEL_USER | KEY_SELECT_USER | Index),
298 StartOpCodeHandle
299 );
300 }
301 Index++;
302 }
303
304 HiiUpdateForm (
305 mCallbackInfo->HiiHandle, // HII handle
306 &mUserProfileManagerGuid, // Formset GUID
307 FORMID_DEL_USER, // Form ID
308 StartOpCodeHandle, // Label for where to insert opcodes
309 EndOpCodeHandle // Replace data
310 );
311
312 HiiFreeOpCodeHandle (StartOpCodeHandle);
313 HiiFreeOpCodeHandle (EndOpCodeHandle);
314 }