2022-07-14 19:41:50 +02:00
|
|
|
|
using RMDataManagerLibrary.DataAcccess;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Web.Http;
|
|
|
|
|
using RMDataManagerLibrary.Models;
|
|
|
|
|
using System.Web;
|
|
|
|
|
using Microsoft.AspNet.Identity;
|
2022-07-22 16:45:33 +02:00
|
|
|
|
using System.Linq;
|
2022-08-20 14:23:25 +02:00
|
|
|
|
using Microsoft.AspNet.Identity.EntityFramework;
|
|
|
|
|
using RMDataManager.Models;
|
2022-07-14 19:41:50 +02:00
|
|
|
|
|
|
|
|
|
namespace RMDataManager.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Authorize]
|
|
|
|
|
public class UserController : ApiController
|
|
|
|
|
{
|
2022-07-22 16:45:33 +02:00
|
|
|
|
[HttpGet]
|
|
|
|
|
public UserModel GetById()
|
2022-07-14 19:41:50 +02:00
|
|
|
|
{
|
|
|
|
|
string userId = RequestContext.Principal.Identity.GetUserId();
|
|
|
|
|
|
|
|
|
|
UserData data = new UserData();
|
|
|
|
|
|
2022-07-22 16:45:33 +02:00
|
|
|
|
return data.GetUserById(userId).First();
|
2022-08-20 14:23:25 +02:00
|
|
|
|
}
|
|
|
|
|
[Authorize(Roles = "Admin")]
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("api/User/Admin/GetAllUsers")]
|
|
|
|
|
public List<ApplicationUserModel> GetAllUsers()
|
|
|
|
|
{
|
|
|
|
|
List<ApplicationUserModel> output = new List<ApplicationUserModel>();
|
|
|
|
|
|
|
|
|
|
using (var context = new ApplicationDbContext())
|
|
|
|
|
{
|
|
|
|
|
var userStore = new UserStore<ApplicationUser>(context);
|
|
|
|
|
var userManager = new UserManager<ApplicationUser>(userStore);
|
|
|
|
|
|
|
|
|
|
var users = userManager.Users.ToList();
|
|
|
|
|
var roles = context.Roles.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var user in users)
|
|
|
|
|
{
|
|
|
|
|
ApplicationUserModel u = new ApplicationUserModel
|
|
|
|
|
{
|
|
|
|
|
Id = user.Id,
|
|
|
|
|
EmailAddress = user.Email
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
foreach (var role in user.Roles)
|
|
|
|
|
{
|
|
|
|
|
u.Roles.Add(role.RoleId, roles.Where(x => x.Id == role.RoleId).First().Name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
output.Add(u);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
2022-07-14 19:41:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|