1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-23 05:55:13 -05:00

Added API for getting the current user's account revision date

This commit is contained in:
Kyle Spearrin 2017-01-14 10:02:37 -05:00
parent fd961dfdf6
commit 4e790fcfa3
7 changed files with 46 additions and 4 deletions

View File

@ -169,6 +169,13 @@ namespace Bit.Api.Controllers
return response; return response;
} }
[HttpGet("revision-date")]
public async Task<DateTime?> GetAccountRevisionDate()
{
var userId = _userService.GetProperUserId(User);
return userId.HasValue ? (await _userService.GetAccountRevisionDateByIdAsync(userId.Value)) : (DateTime?)null;
}
[HttpGet("two-factor")] [HttpGet("two-factor")]
public async Task<TwoFactorResponseModel> GetTwoFactor(string masterPasswordHash, TwoFactorProviderType provider) public async Task<TwoFactorResponseModel> GetTwoFactor(string masterPasswordHash, TwoFactorProviderType provider)
{ {

View File

@ -20,6 +20,7 @@ namespace Bit.Core.Domains
public string TwoFactorRecoveryCode { get; set; } public string TwoFactorRecoveryCode { get; set; }
public string EquivalentDomains { get; set; } public string EquivalentDomains { get; set; }
public string ExcludedGlobalEquivalentDomains { get; set; } public string ExcludedGlobalEquivalentDomains { get; set; }
public DateTime AccountRevisionDate { get; internal set; } = DateTime.UtcNow;
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow; public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow; public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;

View File

@ -153,7 +153,7 @@ namespace Bit.Core.Identity
public async Task<IdentityResult> UpdateAsync(User user, CancellationToken cancellationToken = default(CancellationToken)) public async Task<IdentityResult> UpdateAsync(User user, CancellationToken cancellationToken = default(CancellationToken))
{ {
user.RevisionDate = DateTime.UtcNow; user.RevisionDate = user.AccountRevisionDate = DateTime.UtcNow;
await _userRepository.ReplaceAsync(user); await _userRepository.ReplaceAsync(user);
return IdentityResult.Success; return IdentityResult.Success;
} }

View File

@ -7,5 +7,6 @@ namespace Bit.Core.Repositories
public interface IUserRepository : IRepository<User, Guid> public interface IUserRepository : IRepository<User, Guid>
{ {
Task<User> GetByEmailAsync(string email); Task<User> GetByEmailAsync(string email);
Task<DateTime> GetAccountRevisionDateAsync(Guid id);
} }
} }

View File

@ -36,6 +36,19 @@ namespace Bit.Core.Repositories.SqlServer
} }
} }
public async Task<DateTime> GetAccountRevisionDateAsync(Guid id)
{
using(var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<DateTime>(
$"[{Schema}].[{Table}_ReadAccountRevisionDateById]",
new { Id = id },
commandType: CommandType.StoredProcedure);
return results.SingleOrDefault();
}
}
public override async Task ReplaceAsync(User user) public override async Task ReplaceAsync(User user)
{ {
await base.ReplaceAsync(user); await base.ReplaceAsync(user);

View File

@ -3,13 +3,16 @@ using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Bit.Core.Domains; using Bit.Core.Domains;
using System.Security.Claims;
namespace Bit.Core.Services namespace Bit.Core.Services
{ {
public interface IUserService public interface IUserService
{ {
Guid? GetProperUserId(ClaimsPrincipal principal);
Task<User> GetUserByIdAsync(string userId); Task<User> GetUserByIdAsync(string userId);
Task<User> GetUserByIdAsync(Guid userId); Task<User> GetUserByIdAsync(Guid userId);
Task<DateTime> GetAccountRevisionDateByIdAsync(Guid userId);
Task SaveUserAsync(User user); Task SaveUserAsync(User user);
Task<IdentityResult> RegisterUserAsync(User user, string masterPassword); Task<IdentityResult> RegisterUserAsync(User user, string masterPassword);
Task SendMasterPasswordHintAsync(string email); Task SendMasterPasswordHintAsync(string email);

View File

@ -10,6 +10,7 @@ using System.Linq;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Bit.Core.Enums; using Bit.Core.Enums;
using OtpNet; using OtpNet;
using System.Security.Claims;
namespace Bit.Core.Services namespace Bit.Core.Services
{ {
@ -56,6 +57,17 @@ namespace Bit.Core.Services
_passwordValidators = passwordValidators; _passwordValidators = passwordValidators;
} }
public Guid? GetProperUserId(ClaimsPrincipal principal)
{
Guid userIdGuid;
if(!Guid.TryParse(GetUserId(principal), out userIdGuid))
{
return null;
}
return userIdGuid;
}
public async Task<User> GetUserByIdAsync(string userId) public async Task<User> GetUserByIdAsync(string userId)
{ {
Guid userIdGuid; Guid userIdGuid;
@ -72,6 +84,11 @@ namespace Bit.Core.Services
return await _userRepository.GetByIdAsync(userId); return await _userRepository.GetByIdAsync(userId);
} }
public async Task<DateTime> GetAccountRevisionDateByIdAsync(Guid userId)
{
return await _userRepository.GetAccountRevisionDateAsync(userId);
}
public async Task SaveUserAsync(User user) public async Task SaveUserAsync(User user)
{ {
if(user.Id == default(Guid)) if(user.Id == default(Guid))
@ -79,7 +96,7 @@ namespace Bit.Core.Services
throw new ApplicationException("Use register method to create a new user."); throw new ApplicationException("Use register method to create a new user.");
} }
user.RevisionDate = DateTime.UtcNow; user.RevisionDate = user.AccountRevisionDate = DateTime.UtcNow;
await _userRepository.ReplaceAsync(user); await _userRepository.ReplaceAsync(user);
} }
@ -152,7 +169,7 @@ namespace Bit.Core.Services
user.Email = newEmail; user.Email = newEmail;
user.EmailVerified = true; user.EmailVerified = true;
user.RevisionDate = DateTime.UtcNow; user.RevisionDate = user.AccountRevisionDate = DateTime.UtcNow;
if(ciphers.Any()) if(ciphers.Any())
{ {
@ -186,7 +203,7 @@ namespace Bit.Core.Services
return result; return result;
} }
user.RevisionDate = DateTime.UtcNow; user.RevisionDate = user.AccountRevisionDate = DateTime.UtcNow;
if(ciphers.Any()) if(ciphers.Any())
{ {
await _cipherRepository.UpdateUserEmailPasswordAndCiphersAsync(user, ciphers); await _cipherRepository.UpdateUserEmailPasswordAndCiphersAsync(user, ciphers);