2015-12-09 04:57:38 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2017-03-09 03:45:08 +01:00
|
|
|
|
using Bit.Core.Models.Table;
|
2017-04-05 22:13:40 +02:00
|
|
|
|
using Bit.Core.Enums;
|
2017-12-15 16:50:06 +01:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.Extensions.Primitives;
|
2015-12-09 04:57:38 +01:00
|
|
|
|
|
|
|
|
|
namespace Bit.Core
|
|
|
|
|
{
|
|
|
|
|
public class CurrentContext
|
|
|
|
|
{
|
2017-12-15 16:50:06 +01:00
|
|
|
|
private string _ip;
|
|
|
|
|
|
|
|
|
|
public virtual HttpContext HttpContext { get; set; }
|
2017-12-01 20:06:16 +01:00
|
|
|
|
public virtual Guid? UserId { get; set; }
|
2015-12-09 04:57:38 +01:00
|
|
|
|
public virtual User User { get; set; }
|
2016-08-06 08:29:15 +02:00
|
|
|
|
public virtual string DeviceIdentifier { get; set; }
|
2017-12-15 16:50:06 +01:00
|
|
|
|
public virtual DeviceType? DeviceType { get; set; }
|
|
|
|
|
public virtual string IpAddress => GetRequestIp();
|
2017-04-05 22:13:40 +02:00
|
|
|
|
public virtual List<CurrentContentOrganization> Organizations { get; set; } = new List<CurrentContentOrganization>();
|
2017-08-10 21:26:05 +02:00
|
|
|
|
public virtual Guid? InstallationId { get; set; }
|
2017-04-05 22:13:40 +02:00
|
|
|
|
|
|
|
|
|
public bool OrganizationUser(Guid orgId)
|
|
|
|
|
{
|
|
|
|
|
return Organizations.Any(o => o.Id == orgId);
|
|
|
|
|
}
|
|
|
|
|
public bool OrganizationAdmin(Guid orgId)
|
|
|
|
|
{
|
|
|
|
|
return Organizations.Any(o => o.Id == orgId &&
|
|
|
|
|
(o.Type == OrganizationUserType.Owner || o.Type == OrganizationUserType.Admin));
|
|
|
|
|
}
|
|
|
|
|
public bool OrganizationOwner(Guid orgId)
|
|
|
|
|
{
|
|
|
|
|
return Organizations.Any(o => o.Id == orgId && o.Type == OrganizationUserType.Owner);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-15 16:50:06 +01:00
|
|
|
|
private string GetRequestIp()
|
|
|
|
|
{
|
|
|
|
|
if(!string.IsNullOrWhiteSpace(_ip))
|
|
|
|
|
{
|
|
|
|
|
return _ip;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(HttpContext == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(string.IsNullOrWhiteSpace(_ip))
|
|
|
|
|
{
|
|
|
|
|
_ip = HttpContext.Connection?.RemoteIpAddress?.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _ip;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-05 22:13:40 +02:00
|
|
|
|
public class CurrentContentOrganization
|
|
|
|
|
{
|
|
|
|
|
public Guid Id { get; set; }
|
|
|
|
|
public OrganizationUserType Type { get; set; }
|
|
|
|
|
}
|
2015-12-09 04:57:38 +01:00
|
|
|
|
}
|
|
|
|
|
}
|