1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-04 14:13:28 +01:00
bitwarden-server/src/Admin/Controllers/OrganizationsController.cs

114 lines
3.8 KiB
C#
Raw Normal View History

2018-03-21 22:41:14 +01:00
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Bit.Core.Repositories;
using System.Threading.Tasks;
using Bit.Admin.Models;
using System.Collections.Generic;
using Bit.Core.Models.Table;
using Bit.Core;
2018-03-23 14:44:48 +01:00
using Bit.Core.Utilities;
2018-03-21 22:41:14 +01:00
namespace Bit.Admin.Controllers
{
[Authorize]
public class OrganizationsController : Controller
{
private readonly IOrganizationRepository _organizationRepository;
2018-03-23 02:10:10 +01:00
private readonly IOrganizationUserRepository _organizationUserRepository;
private readonly GlobalSettings _globalSettings;
2018-03-21 22:41:14 +01:00
public OrganizationsController(
IOrganizationRepository organizationRepository,
2018-03-23 02:10:10 +01:00
IOrganizationUserRepository organizationUserRepository,
GlobalSettings globalSettings)
2018-03-21 22:41:14 +01:00
{
_organizationRepository = organizationRepository;
2018-03-23 02:10:10 +01:00
_organizationUserRepository = organizationUserRepository;
_globalSettings = globalSettings;
2018-03-21 22:41:14 +01:00
}
public async Task<IActionResult> Index(string name = null, string userEmail = null, bool? paid = null,
2018-03-21 22:41:14 +01:00
int page = 1, int count = 25)
{
if(page < 1)
{
page = 1;
}
if(count < 1)
{
count = 1;
}
var skip = (page - 1) * count;
var organizations = await _organizationRepository.SearchAsync(name, userEmail, paid, skip, count);
2018-03-21 22:41:14 +01: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 14:48:36 +01:00
Count = count,
2018-03-29 17:26:19 +02:00
Action = _globalSettings.SelfHosted ? "View" : "Edit",
SelfHosted = _globalSettings.SelfHosted
2018-03-21 22:41:14 +01:00
});
}
2018-03-22 19:29:33 +01:00
2018-03-23 14:29:11 +01:00
public async Task<IActionResult> View(Guid id)
{
var organization = await _organizationRepository.GetByIdAsync(id);
if(organization == null)
{
return RedirectToAction("Index");
}
var users = await _organizationUserRepository.GetManyDetailsByOrganizationAsync(id);
return View(new OrganizationViewModel(organization, users));
}
2018-03-23 14:44:48 +01:00
[SelfHosted(NotSelfHostedOnly = true)]
2018-03-22 19:29:33 +01:00
public async Task<IActionResult> Edit(Guid id)
{
var organization = await _organizationRepository.GetByIdAsync(id);
if(organization == null)
{
return RedirectToAction("Index");
}
2018-03-23 02:10:10 +01:00
var users = await _organizationUserRepository.GetManyDetailsByOrganizationAsync(id);
return View(new OrganizationEditModel(organization, users, _globalSettings));
2018-03-22 19:29:33 +01:00
}
[HttpPost]
2018-03-23 02:10:10 +01:00
[ValidateAntiForgeryToken]
2018-03-23 14:44:48 +01:00
[SelfHosted(NotSelfHostedOnly = true)]
2018-03-22 19:29:33 +01:00
public async Task<IActionResult> Edit(Guid id, OrganizationEditModel model)
{
var organization = await _organizationRepository.GetByIdAsync(id);
if(organization == null)
{
return RedirectToAction("Index");
}
model.ToOrganization(organization);
await _organizationRepository.ReplaceAsync(organization);
return RedirectToAction("Edit", new { id });
}
2018-03-22 20:50:56 +01:00
2018-03-23 02:21:57 +01:00
[HttpPost]
[ValidateAntiForgeryToken]
2018-03-22 20:50:56 +01:00
public async Task<IActionResult> Delete(Guid id)
{
var organization = await _organizationRepository.GetByIdAsync(id);
if(organization != null)
{
await _organizationRepository.DeleteAsync(organization);
}
return RedirectToAction("Index");
}
2018-03-21 22:41:14 +01:00
}
}