Created an individual user api endpoint so we can make the user management pages better #865

pull/1488/head
TidusJar 8 years ago
parent 357d760068
commit bad109c862

@ -11,6 +11,7 @@ namespace Ombi.Core.IdentityResolver
Task<bool> CredentialsValid(string username, string password); Task<bool> CredentialsValid(string username, string password);
Task<UserDto> GetUser(string username); Task<UserDto> GetUser(string username);
Task<UserDto> GetUser(int userId);
Task<IEnumerable<UserDto>> GetUsers(); Task<IEnumerable<UserDto>> GetUsers();

@ -36,6 +36,10 @@ namespace Ombi.Core.IdentityResolver
{ {
return Mapper.Map<UserDto>(await UserRepository.GetUser(username)); return Mapper.Map<UserDto>(await UserRepository.GetUser(username));
} }
public async Task<UserDto> GetUser(int userId)
{
return Mapper.Map<UserDto>(await UserRepository.GetUser(userId));
}
public async Task<IEnumerable<UserDto>> GetUsers() public async Task<IEnumerable<UserDto>> GetUsers()
{ {

@ -11,5 +11,6 @@ namespace Ombi.Store.Repository
Task<IEnumerable<User>> GetUsers(); Task<IEnumerable<User>> GetUsers();
Task DeleteUser(User user); Task DeleteUser(User user);
Task<User> UpdateUser(User user); Task<User> UpdateUser(User user);
Task<User> GetUser(int userId);
} }
} }

@ -50,6 +50,13 @@ namespace Ombi.Store.Repository
return user; return user;
} }
public async Task<User> GetUser(int userId)
{
var user = await Db.Users.FirstOrDefaultAsync(x => x.Id == userId);
Db.Entry(user).Reload();
return user;
}
public async Task CreateUser(User user) public async Task CreateUser(User user)
{ {
Db.Users.Add(user); Db.Users.Add(user);

@ -6,7 +6,8 @@ include the remember me checkbox
<div class="container" id="login"> <div class="container" id="login">
<div class="card card-container"> <div class="card card-container">
<!-- <img class="profile-img-card" src="//lh3.googleusercontent.com/-6V8xOA6M7BA/AAAAAAAAAAI/AAAAAAAAAAA/rzlHcD0KYwo/photo.jpg?sz=120" alt="" /> --> <!-- <img class="profile-img-card" src="//lh3.googleusercontent.com/-6V8xOA6M7BA/AAAAAAAAAAI/AAAAAAAAAAA/rzlHcD0KYwo/photo.jpg?sz=120" alt="" /> -->
<img id="profile-img" class="profile-img-card" src="/images/logo.png" /> <div *ngIf="!customizationSettings.logo"><img id="profile-img" class="profile-img-card" src="/images/logo.png" /></div>
<div *ngIf="customizationSettings.logo"><img id="profile-img" class="profile-img-card" [src]="customizationSettings.logo" /></div>
<p id="profile-name" class="profile-name-card"></p> <p id="profile-name" class="profile-name-card"></p>
<form class="form-signin" novalidate [formGroup]="form" (ngSubmit)="onSubmit(form)"> <form class="form-signin" novalidate [formGroup]="form" (ngSubmit)="onSubmit(form)">

@ -5,6 +5,8 @@ import { FormGroup, Validators, FormBuilder } from '@angular/forms';
import { AuthService } from '../auth/auth.service'; import { AuthService } from '../auth/auth.service';
import { StatusService } from '../services/status.service'; import { StatusService } from '../services/status.service';
import { NotificationService } from '../services/notification.service'; import { NotificationService } from '../services/notification.service';
import { SettingsService } from '../services/settings.service';
import { ICustomizationSettings } from '../interfaces/ISettings';
@ -15,7 +17,8 @@ import { NotificationService } from '../services/notification.service';
export class LoginComponent implements OnInit { export class LoginComponent implements OnInit {
constructor(private authService: AuthService, private router: Router, private notify: NotificationService, private status: StatusService, private fb: FormBuilder) { constructor(private authService: AuthService, private router: Router, private notify: NotificationService, private status: StatusService,
private fb: FormBuilder, private settingsService : SettingsService) {
this.form = this.fb.group({ this.form = this.fb.group({
username: ["", [Validators.required]], username: ["", [Validators.required]],
password: ["", [Validators.required]] password: ["", [Validators.required]]
@ -29,9 +32,10 @@ export class LoginComponent implements OnInit {
} }
form: FormGroup; form: FormGroup;
customizationSettings : ICustomizationSettings;
ngOnInit(): void {
ngOnInit(): void {
this.settingsService.getCustomization().subscribe(x => this.customizationSettings = x);
} }

@ -28,7 +28,7 @@ namespace Ombi.Controllers
IdentityManager = identity; IdentityManager = identity;
Mapper = mapper; Mapper = mapper;
} }
private IUserIdentityManager IdentityManager { get; } private IUserIdentityManager IdentityManager { get; }
private IMapper Mapper { get; } private IMapper Mapper { get; }
@ -39,7 +39,7 @@ namespace Ombi.Controllers
[HttpGet] [HttpGet]
public async Task<UserViewModel> GetUser() public async Task<UserViewModel> GetUser()
{ {
return Mapper.Map<UserViewModel>(await IdentityManager.GetUser(this.HttpContext.User.Identity.Name)); return Mapper.Map<UserViewModel>(await IdentityManager.GetUser(this.HttpContext.User.Identity.Name));
} }
@ -68,7 +68,7 @@ namespace Ombi.Controllers
{ {
Username = user.Username, Username = user.Username,
UserType = UserType.LocalUser, UserType = UserType.LocalUser,
Claims = new List<Claim>() {new Claim(ClaimTypes.Role, OmbiClaims.Admin)}, Claims = new List<Claim>() { new Claim(ClaimTypes.Role, OmbiClaims.Admin) },
Password = user.Password, Password = user.Password,
}); });
@ -108,6 +108,38 @@ namespace Ombi.Controllers
return users; return users;
} }
/// <summary>
/// Gets the user by the user id.
/// </summary>
/// <returns>Information about the user</returns>
[HttpGet("Users/{id}")]
public async Task<UserViewModel> GetUser(int id)
{
var type = typeof(OmbiClaims);
FieldInfo[] fieldInfos = type.GetFields(BindingFlags.Public |
BindingFlags.Static | BindingFlags.FlattenHierarchy);
var fields = fieldInfos.Where(fi => fi.IsLiteral && !fi.IsInitOnly).ToList();
var allClaims = fields.Select(x => x.Name).ToList();
var user = Mapper.Map<UserViewModel>(await IdentityManager.GetUser(id)).ToList();
var userClaims = user.Claims.Select(x => x.Value);
IEnumerable<string> left = allClaims.Except(userClaims);
foreach (var c in left)
{
user.Claims.Add(new ClaimCheckboxes
{
Enabled = false,
Value = c
});
}
return user;
}
/// <summary> /// <summary>
/// Creates the user. /// Creates the user.
/// </summary> /// </summary>
@ -159,8 +191,8 @@ namespace Ombi.Controllers
var fields = fieldInfos.Where(fi => fi.IsLiteral && !fi.IsInitOnly).ToList(); var fields = fieldInfos.Where(fi => fi.IsLiteral && !fi.IsInitOnly).ToList();
var allClaims = fields.Select(x => x.Name).ToList(); var allClaims = fields.Select(x => x.Name).ToList();
return allClaims.Select(x => new ClaimCheckboxes() {Value = x}); return allClaims.Select(x => new ClaimCheckboxes() { Value = x });
} }
} }
} }

Loading…
Cancel
Save