From b18b6a44ef29b0b645a87a595b0ac924c14c30fd Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 2 Mar 2017 21:51:03 -0500 Subject: [PATCH] organizations to account profile --- src/Api/Controllers/AccountsController.cs | 9 +++++++-- .../Models/Response/AuthTokenResponseModel.cs | 2 +- .../Models/Response/ProfileResponseModel.cs | 18 +++++++++++++++++- .../SqlServer/OrganizationRepository.cs | 2 +- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/Api/Controllers/AccountsController.cs b/src/Api/Controllers/AccountsController.cs index 8d039e7e25..3492370da9 100644 --- a/src/Api/Controllers/AccountsController.cs +++ b/src/Api/Controllers/AccountsController.cs @@ -9,6 +9,7 @@ using Microsoft.AspNetCore.Identity; using Bit.Core.Domains; using Bit.Core.Enums; using System.Linq; +using Bit.Core.Repositories; namespace Bit.Api.Controllers { @@ -18,15 +19,18 @@ namespace Bit.Api.Controllers { private readonly IUserService _userService; private readonly ICipherService _cipherService; + private readonly IOrganizationRepository _organizationRepository; private readonly UserManager _userManager; public AccountsController( IUserService userService, ICipherService cipherService, + IOrganizationRepository organizationRepository, UserManager userManager) { _userService = userService; _cipherService = cipherService; + _organizationRepository = organizationRepository; _userManager = userManager; } @@ -155,7 +159,8 @@ namespace Bit.Api.Controllers public async Task GetProfile() { var user = await _userService.GetUserByPrincipalAsync(User); - var response = new ProfileResponseModel(user); + var organizations = await _organizationRepository.GetManyByUserIdAsync(user.Id); + var response = new ProfileResponseModel(user, organizations); return response; } @@ -165,7 +170,7 @@ namespace Bit.Api.Controllers { var user = await _userService.GetUserByPrincipalAsync(User); await _userService.SaveUserAsync(model.ToUser(user)); - var response = new ProfileResponseModel(user); + var response = new ProfileResponseModel(user, null); return response; } diff --git a/src/Api/Models/Response/AuthTokenResponseModel.cs b/src/Api/Models/Response/AuthTokenResponseModel.cs index bd740eb94a..4b500769e1 100644 --- a/src/Api/Models/Response/AuthTokenResponseModel.cs +++ b/src/Api/Models/Response/AuthTokenResponseModel.cs @@ -9,7 +9,7 @@ namespace Bit.Api.Models : base("authToken") { Token = token; - Profile = user == null ? null : new ProfileResponseModel(user); + Profile = user == null ? null : new ProfileResponseModel(user, null); } public string Token { get; set; } diff --git a/src/Api/Models/Response/ProfileResponseModel.cs b/src/Api/Models/Response/ProfileResponseModel.cs index d3b3569732..cc4ce58364 100644 --- a/src/Api/Models/Response/ProfileResponseModel.cs +++ b/src/Api/Models/Response/ProfileResponseModel.cs @@ -1,11 +1,13 @@ using System; using Bit.Core.Domains; +using System.Collections.Generic; +using System.Linq; namespace Bit.Api.Models { public class ProfileResponseModel : ResponseModel { - public ProfileResponseModel(User user) + public ProfileResponseModel(User user, IEnumerable organizations) : base("profile") { if(user == null) @@ -19,6 +21,7 @@ namespace Bit.Api.Models MasterPasswordHint = string.IsNullOrWhiteSpace(user.MasterPasswordHint) ? null : user.MasterPasswordHint; Culture = user.Culture; TwoFactorEnabled = user.TwoFactorEnabled; + Organizations = organizations?.Select(o => new OrganizationResponseModel(o)); } public string Id { get; set; } @@ -27,5 +30,18 @@ namespace Bit.Api.Models public string MasterPasswordHint { get; set; } public string Culture { get; set; } public bool TwoFactorEnabled { get; set; } + public IEnumerable Organizations { get; set; } + + public class OrganizationResponseModel + { + public OrganizationResponseModel(Organization organization) + { + Id = organization.Id.ToString(); + Name = organization.Name; + } + + public string Id { get; set; } + public string Name { get; set; } + } } } diff --git a/src/Core/Repositories/SqlServer/OrganizationRepository.cs b/src/Core/Repositories/SqlServer/OrganizationRepository.cs index bd30fab32d..b2c5182900 100644 --- a/src/Core/Repositories/SqlServer/OrganizationRepository.cs +++ b/src/Core/Repositories/SqlServer/OrganizationRepository.cs @@ -37,7 +37,7 @@ namespace Bit.Core.Repositories.SqlServer using(var connection = new SqlConnection(ConnectionString)) { var results = await connection.QueryAsync( - "[dbo].[Organization_ReadUserId]", + "[dbo].[Organization_ReadByUserId]", new { UserId = userId }, commandType: CommandType.StoredProcedure);