Files
server/src/Admin/Controllers/OrganizationsController.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

169 lines
6.9 KiB
C#
Raw Normal View History

2018-03-21 17:41:14 -04:00
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Bit.Admin.Models;
using Bit.Core.Enums;
using Bit.Core.Models.Business;
using Bit.Core.Models.Table;
2018-03-23 09:44:48 -04:00
using Bit.Core.Repositories;
2019-02-25 10:39:04 -05:00
using Bit.Core.Services;
using Bit.Core.Settings;
using Bit.Core.Utilities;
using Microsoft.AspNetCore.Authorization;
2018-03-21 17:41:14 -04:00
using Microsoft.AspNetCore.Mvc;
namespace Bit.Admin.Controllers
{
[Authorize]
public class OrganizationsController : Controller
{
private readonly IOrganizationRepository _organizationRepository;
2018-03-22 21:10:10 -04:00
private readonly IOrganizationUserRepository _organizationUserRepository;
private readonly ICipherRepository _cipherRepository;
private readonly ICollectionRepository _collectionRepository;
private readonly IGroupRepository _groupRepository;
private readonly IPolicyRepository _policyRepository;
2019-02-25 10:39:04 -05:00
private readonly IPaymentService _paymentService;
private readonly IApplicationCacheService _applicationCacheService;
private readonly GlobalSettings _globalSettings;
private readonly IReferenceEventService _referenceEventService;
private readonly IUserService _userService;
2018-03-21 17:41:14 -04:00
public OrganizationsController(
IOrganizationRepository organizationRepository,
2018-03-22 21:10:10 -04:00
IOrganizationUserRepository organizationUserRepository,
ICipherRepository cipherRepository,
ICollectionRepository collectionRepository,
IGroupRepository groupRepository,
IPolicyRepository policyRepository,
2019-02-25 10:39:04 -05:00
IPaymentService paymentService,
IApplicationCacheService applicationCacheService,
GlobalSettings globalSettings,
IReferenceEventService referenceEventService,
IUserService userService)
2018-03-21 17:41:14 -04:00
{
_organizationRepository = organizationRepository;
2018-03-22 21:10:10 -04:00
_organizationUserRepository = organizationUserRepository;
_cipherRepository = cipherRepository;
_collectionRepository = collectionRepository;
_groupRepository = groupRepository;
_policyRepository = policyRepository;
2019-02-25 10:39:04 -05:00
_paymentService = paymentService;
_applicationCacheService = applicationCacheService;
_globalSettings = globalSettings;
_referenceEventService = referenceEventService;
_userService = userService;
2018-03-21 17:41:14 -04:00
}
public async Task<IActionResult> Index(string name = null, string userEmail = null, bool? paid = null,
2018-03-21 17:41:14 -04:00
int page = 1, int count = 25)
{
if (page < 1)
2018-03-21 17:41:14 -04:00
{
page = 1;
}
if (count < 1)
2018-03-21 17:41:14 -04:00
{
count = 1;
}
var skip = (page - 1) * count;
var organizations = await _organizationRepository.SearchAsync(name, userEmail, paid, skip, count);
2018-03-21 17:41:14 -04:00
return View(new OrganizationsModel
{
Items = organizations as List<Organization>,
Name = string.IsNullOrWhiteSpace(name) ? null : name,
UserEmail = string.IsNullOrWhiteSpace(userEmail) ? null : userEmail,
Paid = paid,
Page = page,
2018-03-23 09:48:36 -04:00
Count = count,
2018-03-29 11:26:19 -04:00
Action = _globalSettings.SelfHosted ? "View" : "Edit",
SelfHosted = _globalSettings.SelfHosted
2018-03-21 17:41:14 -04:00
});
}
2018-03-22 14:29:33 -04:00
2018-03-23 09:29:11 -04:00
public async Task<IActionResult> View(Guid id)
{
var organization = await _organizationRepository.GetByIdAsync(id);
if (organization == null)
2018-03-23 09:29:11 -04:00
{
return RedirectToAction("Index");
}
var ciphers = await _cipherRepository.GetManyByOrganizationIdAsync(id);
var collections = await _collectionRepository.GetManyByOrganizationIdAsync(id);
IEnumerable<Group> groups = null;
if (organization.UseGroups)
{
groups = await _groupRepository.GetManyByOrganizationIdAsync(id);
}
IEnumerable<Policy> policies = null;
if (organization.UsePolicies)
{
policies = await _policyRepository.GetManyByOrganizationIdAsync(id);
}
2018-03-23 09:29:11 -04:00
var users = await _organizationUserRepository.GetManyDetailsByOrganizationAsync(id);
return View(new OrganizationViewModel(organization, users, ciphers, collections, groups, policies));
2018-03-23 09:29:11 -04:00
}
2018-03-23 09:44:48 -04:00
[SelfHosted(NotSelfHostedOnly = true)]
2018-03-22 14:29:33 -04:00
public async Task<IActionResult> Edit(Guid id)
{
var organization = await _organizationRepository.GetByIdAsync(id);
if (organization == null)
2018-03-22 14:29:33 -04:00
{
return RedirectToAction("Index");
}
var ciphers = await _cipherRepository.GetManyByOrganizationIdAsync(id);
var collections = await _collectionRepository.GetManyByOrganizationIdAsync(id);
IEnumerable<Group> groups = null;
if (organization.UseGroups)
{
groups = await _groupRepository.GetManyByOrganizationIdAsync(id);
}
IEnumerable<Policy> policies = null;
if (organization.UsePolicies)
{
policies = await _policyRepository.GetManyByOrganizationIdAsync(id);
}
2018-03-22 21:10:10 -04:00
var users = await _organizationUserRepository.GetManyDetailsByOrganizationAsync(id);
2019-02-25 10:39:04 -05:00
var billingInfo = await _paymentService.GetBillingAsync(organization);
return View(new OrganizationEditModel(organization, users, ciphers, collections, groups, policies,
billingInfo, _globalSettings));
2018-03-22 14:29:33 -04:00
}
[HttpPost]
2018-03-22 21:10:10 -04:00
[ValidateAntiForgeryToken]
2018-03-23 09:44:48 -04:00
[SelfHosted(NotSelfHostedOnly = true)]
2018-03-22 14:29:33 -04:00
public async Task<IActionResult> Edit(Guid id, OrganizationEditModel model)
{
var organization = await _organizationRepository.GetByIdAsync(id);
model.ToOrganization(organization);
await _organizationRepository.ReplaceAsync(organization);
await _applicationCacheService.UpsertOrganizationAbilityAsync(organization);
await _referenceEventService.RaiseEventAsync(new ReferenceEvent(ReferenceEventType.OrganizationEditedByAdmin, organization)
{
EventRaisedByUser = _userService.GetUserName(User),
SalesAssistedTrialStarted = model.SalesAssistedTrialStarted,
});
2018-03-22 14:29:33 -04:00
return RedirectToAction("Edit", new { id });
}
2018-03-22 15:50:56 -04:00
2018-03-22 21:21:57 -04:00
[HttpPost]
[ValidateAntiForgeryToken]
2018-03-22 15:50:56 -04:00
public async Task<IActionResult> Delete(Guid id)
{
var organization = await _organizationRepository.GetByIdAsync(id);
if (organization != null)
2018-03-22 15:50:56 -04:00
{
await _organizationRepository.DeleteAsync(organization);
await _applicationCacheService.DeleteOrganizationAbilityAsync(organization.Id);
2018-03-22 15:50:56 -04:00
}
return RedirectToAction("Index");
}
2018-03-21 17:41:14 -04:00
}
}