Skip to content

Commit

Permalink
Kullanıcı güncelleme yapıldı(rolle birlikte )
Browse files Browse the repository at this point in the history
  • Loading branch information
busraozdemir0 committed Aug 17, 2023
1 parent 654183c commit a8b1d9a
Show file tree
Hide file tree
Showing 48 changed files with 142 additions and 6 deletions.
6 changes: 6 additions & 0 deletions Entities/Dtos/UserDtoForUpdate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Entities.Dtos{
public record UserDtoForUpdate:UserDto
{
public HashSet<string> UserRoles { get; set; }=new HashSet<string>();
}
}
Binary file modified Entities/bin/Debug/net6.0/Entities.dll
Binary file not shown.
Binary file modified Entities/bin/Debug/net6.0/Entities.pdb
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9914c797cf9917450009e49555ce84791feb214a
54b68fabd0ddbf00e9e3ca968ad4d1cb2174c4b8
Binary file modified Entities/obj/Debug/net6.0/Entities.dll
Binary file not shown.
Binary file modified Entities/obj/Debug/net6.0/Entities.pdb
Binary file not shown.
Binary file modified Entities/obj/Debug/net6.0/ref/Entities.dll
Binary file not shown.
Binary file modified Entities/obj/Debug/net6.0/refint/Entities.dll
Binary file not shown.
Binary file modified ProductDb.db-wal
Binary file not shown.
Binary file modified Repositories/bin/Debug/net6.0/Entities.dll
Binary file not shown.
Binary file modified Repositories/bin/Debug/net6.0/Entities.pdb
Binary file not shown.
Binary file modified Repositories/bin/Debug/net6.0/Repositories.dll
Binary file not shown.
Binary file modified Repositories/bin/Debug/net6.0/Repositories.pdb
Binary file not shown.
Binary file not shown.
Binary file modified Repositories/obj/Debug/net6.0/Repositories.dll
Binary file not shown.
Binary file modified Repositories/obj/Debug/net6.0/Repositories.pdb
Binary file not shown.
51 changes: 48 additions & 3 deletions Services/AuthManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Runtime.CompilerServices;
using AutoMapper;
using Entities.Dtos;
using Microsoft.AspNetCore.Identity;
Expand All @@ -11,11 +12,15 @@ public class AuthManager : IAuthService
private readonly UserManager<IdentityUser> _userManager;
private readonly IMapper _mapper;

public AuthManager(RoleManager<IdentityRole> roleManager, UserManager<IdentityUser> userManager,IMapper mapper)
public AuthManager(
RoleManager<IdentityRole> roleManager,
UserManager<IdentityUser> userManager,
IMapper mapper
)
{
_roleManager = roleManager;
_userManager = userManager;
_mapper=mapper;
_mapper = mapper;
}

public IEnumerable<IdentityRole> Roles => _roleManager.Roles;
Expand All @@ -42,5 +47,45 @@ public IEnumerable<IdentityUser> GetAllUsers()
{
return _userManager.Users.ToList();
}

public async Task<IdentityUser> GetOneUser(string userName)
{
return await _userManager.FindByNameAsync(userName); // userName göre bulma
}

public async Task<UserDtoForUpdate> GetOneUserForUpdate(string userName)
{
var user= await GetOneUser(userName);
if(user is not null)
{
var userDto=_mapper.Map<UserDtoForUpdate>(user); // mapleme
userDto.Roles=new HashSet<string>(Roles.Select(r=>r.Name).ToList()); // bütün rolleri aldık
userDto.UserRoles=new HashSet<string>(await _userManager.GetRolesAsync(user)); // gönderilen kullanıcının rolleri
return userDto;
}
throw new Exception("An error occured.");
}

public async Task Update(UserDtoForUpdate userDto)
{
var user = await GetOneUser(userDto.UserName);
user.PhoneNumber = userDto.PhoneNumber;
user.Email = userDto.Email;

if (user is not null) // böyle bir kullanıcı varsa güncelleme işlemine geç
{
var result = await _userManager.UpdateAsync(user);
if (userDto.Roles.Count > 0)
{
var userRoles = await _userManager.GetRolesAsync(user); // kullanıcı hangi rollere aitse o rolleri aldık
var r1 = await _userManager.RemoveFromRolesAsync(user, userRoles); // kullanıcının rollerini bu satır ile kaldırdık(örn. user rolü kaldırıldı)
var r2 = await _userManager.AddToRolesAsync(user, userDto.Roles); // belirtilen kullanıcının rollerini kaldırtıktan sonra yeni rolleri atadık(örn. kaldırılan user rolü yerine hem user hem editör eklendi)
}
return;
}
throw new Exception("System has problem with user update.");


}
}
}
}
3 changes: 3 additions & 0 deletions Services/Contracts/IAuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ public interface IAuthService
{
IEnumerable<IdentityRole> Roles{get;} // sistemdeki tüm rolleri getirmek için
IEnumerable<IdentityUser> GetAllUsers(); // sistemdeki tüm kullanıcıları getirmek için
Task<IdentityUser> GetOneUser(string userName);
Task<UserDtoForUpdate> GetOneUserForUpdate(string userName);
Task<IdentityResult> CreateUser(UserDtoForCreation userDto);
Task Update (UserDtoForUpdate userDto);
}
}
Binary file modified Services/bin/Debug/net6.0/Entities.dll
Binary file not shown.
Binary file modified Services/bin/Debug/net6.0/Entities.pdb
Binary file not shown.
Binary file modified Services/bin/Debug/net6.0/Repositories.dll
Binary file not shown.
Binary file modified Services/bin/Debug/net6.0/Repositories.pdb
Binary file not shown.
Binary file modified Services/bin/Debug/net6.0/Services.dll
Binary file not shown.
Binary file modified Services/bin/Debug/net6.0/Services.pdb
Binary file not shown.
Binary file not shown.
Binary file modified Services/obj/Debug/net6.0/Services.dll
Binary file not shown.
Binary file modified Services/obj/Debug/net6.0/Services.pdb
Binary file not shown.
Binary file modified Services/obj/Debug/net6.0/ref/Services.dll
Binary file not shown.
Binary file modified Services/obj/Debug/net6.0/refint/Services.dll
Binary file not shown.
17 changes: 17 additions & 0 deletions StoreApp/Areas/Admin/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,22 @@ public async Task<IActionResult> Create([FromForm] UserDtoForCreation userDto)
? RedirectToAction("Index")
: View();
}

public async Task<IActionResult> Update([FromRoute(Name ="id")]string id)
{
var user=await _manager.AuthService.GetOneUserForUpdate(id);
return View(user);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Update([FromForm]UserDtoForUpdate userDto)
{
if(ModelState.IsValid)
{
await _manager.AuthService.Update(userDto);
return RedirectToAction("Index");
}
return View();
}
}
}
7 changes: 5 additions & 2 deletions StoreApp/Areas/Admin/Views/User/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
<div class="text-center my-5">
<div class="display-6 my-4">Users</div>
</div>

<div class="d-flex justify-content-end my-3">
<a class="btn btn-outline-success" asp-action="Create">
<i class="fa fa-plus"></i>Add User</a>
</div>
<table>
<thead>
<tr>
Expand All @@ -22,7 +25,7 @@
<td>-</td>
<td>
<div class="btn-group">
<a class="btn btn-sm btn-warning">Update</a>
<a class="btn btn-sm btn-warning" asp-action="Update" asp-route-id="@user.UserName">Edit</a>
<a class="btn btn-sm btn-info">Reset Password</a>
<a class="btn btn-sm btn-danger">Delete</a>
</div>
Expand Down
52 changes: 52 additions & 0 deletions StoreApp/Areas/Admin/Views/User/Update.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
@model UserDtoForUpdate

<div class="text-center my-5">
<div class="display-6 my-4">Update (@Model.UserName)</div>
</div>

<form method="post" asp-action="Update">
<div class="row">
<div class="col-md-6">
<div class="form-group my-3">
<label asp-for="UserName"></label>
<input asp-for="UserName" class="form-control" />
</div>

<div class="form-group my-3">
<label asp-for="Email"></label>
<input asp-for="Email" class="form-control" />
</div>

<div class="form-group my-3">
<label asp-for="PhoneNumber"></label>
<input asp-for="PhoneNumber" class="form-control" />
</div>

<div class="form-group my-3">
<div class="btn-group d-flex justify-content-end">
<input type="submit" class="btn btn-primary" value="Submit" />
<a asp-action="Index" class="btn btn-outline-primary">
<i class="fa fa-solid fa-arrow-left"></i>
Back
</a>
</div>
</div>
</div>

<div class="col-md-6">
<div class="lead p-2 mt-2">
Roles
@foreach (var item in Model.Roles)
{
<div class="form-check my-2">
<input type="checkbox" name="Roles" value="@item" checked="@(Model.UserRoles.Contains(@item))" class="form-check-input" />
<label class="form-check-label">@item</label>
</div>
}
</div>
<div class="text-muted">
Please choose the roles for the user
</div>
</div>
</div>
</form>
1 change: 1 addition & 0 deletions StoreApp/Infrastructure/Mapper/MappingProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public MappingProfile()
CreateMap<ProductDtoForInsertion, Product>(); //Mapleme işlemi otomatik olarak gerçekleşerek dto, Product nesnesine dönüştürülecek
CreateMap<ProductDtoForUpdate, Product>().ReverseMap();
CreateMap<UserDtoForCreation, IdentityUser>();
CreateMap<UserDtoForUpdate, IdentityUser>().ReverseMap();
}
}
}
Binary file modified StoreApp/bin/Debug/net6.0/Entities.dll
Binary file not shown.
Binary file modified StoreApp/bin/Debug/net6.0/Entities.pdb
Binary file not shown.
Binary file modified StoreApp/bin/Debug/net6.0/Repositories.dll
Binary file not shown.
Binary file modified StoreApp/bin/Debug/net6.0/Repositories.pdb
Binary file not shown.
Binary file modified StoreApp/bin/Debug/net6.0/Services.dll
Binary file not shown.
Binary file modified StoreApp/bin/Debug/net6.0/Services.pdb
Binary file not shown.
Binary file modified StoreApp/bin/Debug/net6.0/StoreApp.dll
Binary file not shown.
Binary file modified StoreApp/bin/Debug/net6.0/StoreApp.pdb
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ build_metadata.AdditionalFiles.CssScope =
build_metadata.AdditionalFiles.TargetPath = QXJlYXNcQWRtaW5cVmlld3NcVXNlclxJbmRleC5jc2h0bWw=
build_metadata.AdditionalFiles.CssScope =

[D:/Store/StoreApp/Areas/Admin/Views/User/Update.cshtml]
build_metadata.AdditionalFiles.TargetPath = QXJlYXNcQWRtaW5cVmlld3NcVXNlclxVcGRhdGUuY3NodG1s
build_metadata.AdditionalFiles.CssScope =

[D:/Store/StoreApp/Areas/Admin/Views/_ViewImports.cshtml]
build_metadata.AdditionalFiles.TargetPath = QXJlYXNcQWRtaW5cVmlld3NcX1ZpZXdJbXBvcnRzLmNzaHRtbA==
build_metadata.AdditionalFiles.CssScope =
Expand Down
Binary file modified StoreApp/obj/Debug/net6.0/StoreApp.csproj.AssemblyReference.cache
Binary file not shown.
Binary file modified StoreApp/obj/Debug/net6.0/StoreApp.dll
Binary file not shown.
Binary file modified StoreApp/obj/Debug/net6.0/StoreApp.pdb
Binary file not shown.
5 changes: 5 additions & 0 deletions StoreApp/obj/Debug/net6.0/project.razor.vscode.json
Original file line number Diff line number Diff line change
Expand Up @@ -24112,6 +24112,11 @@
"FilePath": "d:\\Store\\StoreApp\\Areas\\Admin\\Views\\User\\Create.cshtml",
"TargetPath": "Areas\\Admin\\Views\\User\\Create.cshtml",
"FileKind": "mvc"
},
{
"FilePath": "d:\\Store\\StoreApp\\Areas\\Admin\\Views\\User\\Update.cshtml",
"TargetPath": "Areas\\Admin\\Views\\User\\Update.cshtml",
"FileKind": "mvc"
}
]
}
Binary file modified StoreApp/obj/Debug/net6.0/ref/StoreApp.dll
Binary file not shown.
Binary file modified StoreApp/obj/Debug/net6.0/refint/StoreApp.dll
Binary file not shown.

0 comments on commit a8b1d9a

Please sign in to comment.