Skip to content

Commit 802e725

Browse files
committed
完成用户删除功能。
1 parent 96ca942 commit 802e725

File tree

7 files changed

+144
-156
lines changed

7 files changed

+144
-156
lines changed

StudentManagement/StudentManagement/Controllers/AdminController.cs

Lines changed: 77 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -3,86 +3,68 @@
33
using Microsoft.AspNetCore.Mvc;
44
using StudentManagement.Models;
55
using StudentManagement.ViewModels;
6-
using System;
76
using System.Collections.Generic;
87
using System.Linq;
98
using System.Threading.Tasks;
109

1110
namespace StudentManagement.Controllers
1211
{
13-
[Authorize(Roles ="Admin")]
14-
public class AdminController:Controller
12+
[Authorize(Roles = "Admin")]
13+
public class AdminController : Controller
1514
{
1615
private RoleManager<IdentityRole> roleManager;
1716
private UserManager<ApplicationUser> userManager;
1817

19-
public AdminController(RoleManager<IdentityRole> roleManager,UserManager<ApplicationUser> userManager)
18+
public AdminController(RoleManager<IdentityRole> roleManager, UserManager<ApplicationUser> userManager)
2019
{
2120
this.roleManager = roleManager;
2221
this.userManager = userManager;
2322
}
2423

25-
26-
27-
2824
#region 角色管理
2925

30-
31-
32-
// GET Post
26+
// GET Post
3327

3428
[HttpGet]
35-
public IActionResult CreateRole()
29+
public IActionResult CreateRole()
3630
{
37-
3831
return View();
3932
}
4033

41-
4234
[HttpPost]
4335
public async Task<IActionResult> CreateRole(CreateRoleViewModel model)
4436
{
4537
if (ModelState.IsValid)
4638
{
47-
4839
IdentityRole identityRole = new IdentityRole
4940
{
5041
Name = model.RoleName
5142
};
5243

5344
//如果您尝试创建具有已存在的同名的角色,则会收到验证错误。
54-
IdentityResult result = await roleManager.CreateAsync(identityRole);
45+
IdentityResult result = await roleManager.CreateAsync(identityRole);
5546

5647
if (result.Succeeded)
5748
{
5849
return RedirectToAction("ListRoles", "Admin");
5950
}
6051

61-
6252
foreach (var error in result.Errors)
6353
{
6454
ModelState.AddModelError("", error.Description);
6555
}
66-
67-
6856
}
6957

70-
71-
7258
return View(model);
7359
}
7460

75-
76-
7761
public IActionResult ListRoles()
7862
{
79-
8063
var roles = roleManager.Roles;
8164

8265
return View(roles);
8366
}
8467

85-
8668
[HttpGet]
8769
public async Task<IActionResult> EditRole(string id)
8870
{
@@ -105,23 +87,19 @@ public async Task<IActionResult> EditRole(string id)
10587
//如果用户拥有此角色,请将用户名添加到
10688
//EditRoleViewModel模型中的Users属性中
10789

108-
if (await userManager.IsInRoleAsync(user,role.Name))
90+
if (await userManager.IsInRoleAsync(user, role.Name))
10991
{
11092
model.Users.Add(user.UserName);
11193
}
11294
}
11395
//然后将对象传递给视图显示到客户端
11496
return View(model);
115-
11697
}
11798

118-
119-
120-
12199
[HttpPost]
122100
public async Task<IActionResult> EditRole(EditRoleViewModel model)
123101
{
124-
var role =await roleManager.FindByIdAsync(model.Id);
102+
var role = await roleManager.FindByIdAsync(model.Id);
125103

126104
if (role == null)
127105
{
@@ -132,7 +110,7 @@ public async Task<IActionResult> EditRole(EditRoleViewModel model)
132110
{
133111
role.Name = model.RoleName;
134112

135-
var result= await roleManager.UpdateAsync(role);
113+
var result = await roleManager.UpdateAsync(role);
136114

137115
if (result.Succeeded)
138116
{
@@ -141,31 +119,18 @@ public async Task<IActionResult> EditRole(EditRoleViewModel model)
141119

142120
foreach (var error in result.Errors)
143121
{
144-
145122
ModelState.AddModelError("", error.Description);
146-
147123
}
148124

149-
150125
return View(model);
151-
152126
}
153-
154-
155-
156-
157-
158-
159-
160127
}
161128

162-
163129
[HttpGet]
164130
public async Task<IActionResult> EditUsersInRole(string roleId)
165131
{
166132
ViewBag.roleId = roleId;
167133

168-
169134
var role = await roleManager.FindByIdAsync(roleId);
170135

171136
if (role == null)
@@ -176,10 +141,8 @@ public async Task<IActionResult> EditUsersInRole(string roleId)
176141

177142
var model = new List<UserRoleViewModel>();
178143

179-
180144
foreach (var user in userManager.Users)
181145
{
182-
183146
var userRoleViewmodel = new UserRoleViewModel
184147
{
185148
UserId = user.Id,
@@ -193,48 +156,40 @@ public async Task<IActionResult> EditUsersInRole(string roleId)
193156
else
194157
{
195158
userRoleViewmodel.IsSelected = false;
196-
197159
}
198160

199-
200-
model.Add(userRoleViewmodel);
201-
161+
model.Add(userRoleViewmodel);
202162
}
203163

204-
205-
206164
return View(model);
207-
208165
}
209166

210167
[HttpPost]
211168
public async Task<IActionResult> EditUsersInRole(List<UserRoleViewModel> model, string roleId)
212169
{
213-
214170
var role = await roleManager.FindByIdAsync(roleId);
215171
if (role == null)
216172
{
217173
ViewBag.ErrorMessage = $"角色id为{roleId}的信息不存在,请重试。";
218174
return View("NotFound");
219175
}
220176

221-
222177
for (int i = 0; i < model.Count; i++)
223178
{
224-
var user = await userManager.FindByIdAsync(model[i].UserId);
179+
var user = await userManager.FindByIdAsync(model[i].UserId);
225180

226-
var isInRole = await userManager.IsInRoleAsync(user,role.Name);
181+
var isInRole = await userManager.IsInRoleAsync(user, role.Name);
227182
IdentityResult result = null;
228183

229184
//被选中,不属于该角色,这个时候,添加到角色中。
230-
if (model[i].IsSelected&&!isInRole)
185+
if (model[i].IsSelected && !isInRole)
231186
{
232-
result =await userManager.AddToRoleAsync(user,role.Name);
187+
result = await userManager.AddToRoleAsync(user, role.Name);
233188
}
234189
//没有被选中,但是用户已经在角色中,移除出来
235-
else if (!model[i].IsSelected&&isInRole)
190+
else if (!model[i].IsSelected && isInRole)
236191
{
237-
result =await userManager.RemoveFromRoleAsync(user,role.Name);
192+
result = await userManager.RemoveFromRoleAsync(user, role.Name);
238193
}
239194
else//被选中,已经存角色中,不发生任何改变的数据
240195
{
@@ -244,52 +199,41 @@ public async Task<IActionResult> EditUsersInRole(List<UserRoleViewModel> model,
244199
if (result.Succeeded)
245200
{
246201
//7个总用户数,0开始进行索引。
247-
if (i<(model.Count-1))
202+
if (i < (model.Count - 1))
248203
{
249204
continue;
250205
}
251206
else
252207
{
253208
return RedirectToAction("EditRole", new { id = roleId });
254-
255209
}
256-
257-
258-
}
259-
260-
261-
210+
}
262211
}
263212

264213
return RedirectToAction("EditRole", new { id = roleId });
265214
}
266215

267-
268-
#endregion
269-
216+
#endregion 角色管理
270217

271218
#region 用户管理
272219

273220
[HttpGet]
274221
public IActionResult ListUsers()
275222
{
276-
277223
var users = userManager.Users.ToList();
278-
279-
280224

281225
return View(users);
282-
283226
}
284227

285-
286-
287-
[HttpGet]
288-
public async Task<IActionResult> EditUser(string id)
228+
[HttpGet]
229+
public async Task<IActionResult> EditUser(string id)
289230
{
290-
var user =await userManager.FindByIdAsync(id);
231+
232+
233+
234+
var user = await userManager.FindByIdAsync(id);
291235

292-
if (user==null)
236+
if (user == null)
293237
{
294238
ViewBag.ErrorMessage = $"无法找到ID{id}的用户";
295239
return View("NotFound");
@@ -299,54 +243,77 @@ public async Task<IActionResult> EditUser(string id)
299243

300244
var userRoles = await userManager.GetRolesAsync(user);
301245

302-
var model = new EditUserViewModel() {
303-
Id=user.Id,
304-
Email=user.Email,
305-
UserName=user.UserName,
306-
City=user.City,
307-
Claims=userClaims.Select(c=>c.Value).ToList(),
246+
var model = new EditUserViewModel()
247+
{
248+
Id = user.Id,
249+
Email = user.Email,
250+
UserName = user.UserName,
251+
City = user.City,
252+
Claims = userClaims.Select(c => c.Value).ToList(),
308253
Roles = userRoles
309254
};
310-
255+
311256
return View(model);
312-
313257
}
314258

315-
316-
317259
[HttpPost]
318-
public async Task<IActionResult> EditUser(EditUserViewModel model){
260+
public async Task<IActionResult> EditUser(EditUserViewModel model)
261+
{
262+
if (ModelState.IsValid)
263+
{
264+
var user = await userManager.FindByIdAsync(model.Id);
265+
if (user == null)
266+
{
267+
ViewBag.ErrorMessage = $"无法找到ID{model.Id}的用户";
268+
return View("NotFound");
269+
}
270+
else
271+
{
272+
user.Email = model.Email;
273+
user.UserName = model.UserName;
274+
user.City = model.City;
275+
var result = await userManager.UpdateAsync(user);
276+
if (result.Succeeded)
277+
{
278+
return RedirectToAction("ListUsers");
279+
}
280+
foreach (var item in result.Errors)
281+
{
282+
ModelState.AddModelError("", item.Description);
283+
}
284+
}
285+
}
286+
return View(model);
287+
288+
}
319289

320-
var user = await userManager.FindByIdAsync(model.Id);
290+
[HttpPost]
291+
public async Task<IActionResult> DeleteUser(string id)
292+
{
293+
var user = await userManager.FindByIdAsync(id);
321294
if (user == null)
322295
{
323-
ViewBag.ErrorMessage = $"无法找到ID{model.Id}的用户";
296+
ViewBag.ErrorMessage = $"无法找到ID{id}的用户";
324297
return View("NotFound");
325298
}
326299
else
327300
{
328-
user.Email = model.Email;
329-
user.UserName = model.UserName;
330-
user.City = model.City;
331-
var result = await userManager.UpdateAsync(user);
301+
var result = await userManager.DeleteAsync(user);
302+
332303
if (result.Succeeded)
333304
{
334305
return RedirectToAction("ListUsers");
335306
}
336-
foreach (var item in result.Errors)
307+
308+
foreach (var error in result.Errors)
337309
{
338-
ModelState.AddModelError("", item.Description);
310+
ModelState.AddModelError("", error.Description);
339311
}
340-
341-
return View(model);
342-
343312
}
344-
}
345-
346-
347-
#endregion
348-
349313

314+
return View("ListUsers");
315+
}
350316

317+
#endregion 用户管理
351318
}
352-
}
319+
}

0 commit comments

Comments
 (0)