351 lines
12 KiB
C#
351 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection.Emit;
|
|
using System.Security.Cryptography;
|
|
using System.Security.Permissions;
|
|
using System.Text;
|
|
using System.Xml.Serialization;
|
|
using Swarmops.Basic.Types;
|
|
using Swarmops.Basic.Types.Security;
|
|
using Swarmops.Basic.Types.Swarm;
|
|
using Swarmops.Common.Enums;
|
|
using Swarmops.Logic.Structure;
|
|
using Swarmops.Logic.Support;
|
|
using Swarmops.Logic.Swarm;
|
|
|
|
namespace Swarmops.Logic.Security
|
|
{
|
|
public class Authority
|
|
{
|
|
public static Authority FromXml (string xml)
|
|
{
|
|
return new Authority (AuthorityData.FromXml (xml));
|
|
}
|
|
|
|
public static Authority FromEncryptedXml (string cryptXml)
|
|
{
|
|
byte[] keyBytes = Encoding.ASCII.GetBytes(SystemSettings.InstallationId.Replace("-", "")); // unique for this install
|
|
byte[] cryptoBytes = Convert.FromBase64String(cryptXml);
|
|
|
|
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
|
|
{
|
|
aes.Key = keyBytes;
|
|
aes.IV = new ArraySegment<byte> (cryptoBytes, 0, 16).ToArray();
|
|
cryptoBytes = new ArraySegment<byte>(cryptoBytes, 16, cryptoBytes.Length - 16).ToArray();
|
|
|
|
using (ICryptoTransform crypto = aes.CreateDecryptor())
|
|
{
|
|
byte[] clearBytes = crypto.TransformFinalBlock(cryptoBytes, 0, cryptoBytes.Length);
|
|
return FromXml(Encoding.UTF8.GetString (clearBytes));
|
|
}
|
|
}
|
|
}
|
|
|
|
public string ToXml()
|
|
{
|
|
return _data.ToXml();
|
|
}
|
|
|
|
public string ToEncryptedXml()
|
|
{
|
|
byte[] keyBytes = Encoding.ASCII.GetBytes(SystemSettings.InstallationId.Replace ("-","")); // unique for this install
|
|
byte[] dataBytes = Encoding.UTF8.GetBytes (ToXml());
|
|
|
|
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
|
|
{
|
|
aes.Key = keyBytes;
|
|
aes.GenerateIV(); // unique for every encryption
|
|
|
|
if (aes.IV.Length != 16)
|
|
{
|
|
throw new InvalidDataException("IV is not 16 bytes long");
|
|
}
|
|
|
|
using (ICryptoTransform crypto = aes.CreateEncryptor())
|
|
{
|
|
byte[] cryptoBytes = crypto.TransformFinalBlock (dataBytes, 0, dataBytes.Length);
|
|
return Convert.ToBase64String (aes.IV.Concat (cryptoBytes).ToArray()); // joins two byte[] arrays
|
|
}
|
|
}
|
|
}
|
|
|
|
private Authority (AuthorityData data)
|
|
{
|
|
this._data = data;
|
|
}
|
|
|
|
public Person Person
|
|
{
|
|
get { return Person.FromIdentity (_data.PersonId); }
|
|
}
|
|
|
|
public Organization Organization
|
|
{
|
|
get { return Organization.FromIdentity (_data.OrganizationId); }
|
|
}
|
|
|
|
public void SetOrganization (Organization organization)
|
|
{
|
|
// This changes the active organization. There may be a Primary Position in this
|
|
// organization; if so, activate that too. Otherwise, just null out the Position.
|
|
|
|
// The reason this isn't an ordinary setter for the field is to minimize risk for
|
|
// miscoding in the security framework.
|
|
|
|
PositionAssignment primaryAssignment = Person.GetPrimaryAssignment (organization);
|
|
|
|
_data.OrganizationId = organization.Identity;
|
|
_data.PositionAssignmentId = (primaryAssignment == null ? 0 : primaryAssignment.Identity);
|
|
}
|
|
|
|
public void SetPosition (PositionAssignment assignment)
|
|
{
|
|
// This changes to a Position. The Organization will change along with it, but only
|
|
// if it's not a system-level Position.
|
|
|
|
// The reason this isn't an ordinary setter for the field is to minimize risk for
|
|
// miscoding in the security framework.
|
|
|
|
_data.PositionAssignmentId = (assignment == null ? 0 : assignment.Identity);
|
|
if (assignment != null)
|
|
{
|
|
if (assignment.Position.PositionLevel != PositionLevel.Systemwide)
|
|
{
|
|
_data.OrganizationId = assignment.Position.OrganizationId;
|
|
}
|
|
}
|
|
}
|
|
|
|
public PositionAssignment Assignment
|
|
{
|
|
get
|
|
{
|
|
if (_data.PositionAssignmentId == 0)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return PositionAssignment.FromIdentity (_data.PositionAssignmentId);
|
|
}
|
|
}
|
|
|
|
public Position Position
|
|
{
|
|
get { return Assignment != null? Assignment.Position: null; }
|
|
}
|
|
|
|
public static Authority FromLogin(Person person)
|
|
{
|
|
int lastOrgId = person.LastLogonOrganizationId;
|
|
PositionAssignment assignment = null;
|
|
|
|
if (lastOrgId != 0)
|
|
{
|
|
Organization organization = Organization.FromIdentity(lastOrgId);
|
|
assignment = person.GetPrimaryAssignment(organization);
|
|
}
|
|
|
|
// TODO: Verify membership OR position OR volunteer
|
|
|
|
return new Authority(new AuthorityData
|
|
{
|
|
CustomData = new Basic.Types.Common.SerializableDictionary<string, string>(),
|
|
LoginDateTimeUtc = DateTime.UtcNow,
|
|
OrganizationId = lastOrgId,
|
|
PersonId = person.Identity,
|
|
PositionAssignmentId = (assignment != null ? assignment.Identity : 0)
|
|
});
|
|
}
|
|
|
|
public static Authority FromLogin(Person person, Organization organization)
|
|
{
|
|
PositionAssignment assignment = person.GetPrimaryAssignment(organization);
|
|
|
|
// TODO: Verify membership OR position OR volunteer
|
|
|
|
return new Authority(new AuthorityData
|
|
{
|
|
CustomData = new Basic.Types.Common.SerializableDictionary<string, string>(),
|
|
LoginDateTimeUtc = DateTime.UtcNow,
|
|
OrganizationId = organization.Identity,
|
|
PersonId = person.Identity,
|
|
PositionAssignmentId = (assignment != null ? assignment.Identity : 0)
|
|
});
|
|
}
|
|
|
|
private readonly AuthorityData _data;
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Determines if this Authority has a particular Access.
|
|
/// </summary>
|
|
/// <param name="access">The access desired.</param>
|
|
/// <returns>True if access can be granted.</returns>
|
|
public bool HasAccess(Access access)
|
|
{
|
|
if (access == null)
|
|
{
|
|
throw new ArgumentNullException("access", @"Access cannot be null, but must always be explicitly specified. Specify AccessAspect.Null if null access is desired.");
|
|
}
|
|
|
|
if (access.Aspect == AccessAspect.Null)
|
|
{
|
|
// Null security (like Dashboard), so return true
|
|
|
|
return true;
|
|
}
|
|
|
|
// Check for participant financials
|
|
|
|
if (access.Aspect == AccessAspect.Financials && access.Type == AccessType.Read)
|
|
{
|
|
if (access.Organization.ParticipantFinancialsEnabled)
|
|
{
|
|
// This organization has decided to open its financial reports to all participants. Reselect the access request to "participant" level.
|
|
|
|
access = new Access(access.Organization, AccessAspect.Participant);
|
|
}
|
|
}
|
|
|
|
// Check for Participant access level
|
|
|
|
if (access.Aspect == AccessAspect.Participant)
|
|
{
|
|
// Check that a membership (or whatever this org calls it) exists, for this org or a parentline org
|
|
|
|
if (Person.MemberOfWithInherited(access.Organization))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
// if Open Ledgers, return true
|
|
|
|
if ((access.Aspect == AccessAspect.Bookkeeping || access.Aspect == AccessAspect.Financials) &&
|
|
access.Type == AccessType.Read && this.Person.Identity == Swarm.Person.OpenLedgersIdentity)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
// We're at the end of generic access control - now, check against position assignments
|
|
|
|
// Check if the person is currently acting at sysadmin level
|
|
|
|
// TODO: CHECK POSITION ASSIGNMENT
|
|
// for now, check if position assignment exists
|
|
|
|
// This is not very efficient, but there shouldn't be many system-level positions or
|
|
// assignments, so it should scale reasonably even in an 1M organization
|
|
|
|
PositionAssignments assignments = Positions.ForSystem().Assignments;
|
|
|
|
foreach (PositionAssignment assignment in assignments)
|
|
{
|
|
if (assignment.Active && assignment.PersonId == this._data.PersonId)
|
|
{
|
|
if ((assignment.Position.PositionType == PositionType.System_SysadminMain ||
|
|
assignment.Position.PositionType == PositionType.System_SysadminReadWrite))
|
|
return true;
|
|
|
|
if (assignment.Position.PositionType == PositionType.System_SysadminAssistantReadOnly &&
|
|
access.Type == AccessType.Read)
|
|
{
|
|
return true; // Read-only access
|
|
}
|
|
}
|
|
}
|
|
|
|
// If system-level access was requested and has not been granted at this point, deny it
|
|
|
|
if (access.Organization == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Organization-level or geography-level access requested
|
|
|
|
if (Assignment == null)
|
|
{
|
|
// No assignment to ask, therefore, no access
|
|
|
|
return false;
|
|
}
|
|
|
|
// Ask the current position assignment if it has the requested access
|
|
|
|
return Assignment.Position.HasAccess(access);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool CanSeePerson (Person person)
|
|
{
|
|
People initialList = People.FromSingle (person);
|
|
|
|
throw new NotImplementedException();
|
|
|
|
/*People filteredList = Authorization.FilterPeopleToMatchAuthority (initialList, this);
|
|
|
|
if (filteredList.Count == 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;*/
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
[Serializable]
|
|
public class AuthorityData
|
|
{
|
|
public int PersonId { get; set; }
|
|
public int OrganizationId { get; set; }
|
|
public int PositionAssignmentId { get; set; }
|
|
public DateTime LoginDateTimeUtc { get; set; }
|
|
public Basic.Types.Common.SerializableDictionary<string,string> CustomData { get; set; }
|
|
|
|
internal string ToXml()
|
|
{
|
|
XmlSerializer serializer = new XmlSerializer(GetType());
|
|
|
|
MemoryStream stream = new MemoryStream();
|
|
serializer.Serialize(stream, this);
|
|
|
|
byte[] xmlBytes = stream.GetBuffer();
|
|
return Encoding.UTF8.GetString(xmlBytes);
|
|
}
|
|
|
|
static internal AuthorityData FromXml (string xml)
|
|
{
|
|
// Compensate for stupid Mono encoding bugs
|
|
|
|
if (xml.StartsWith("?"))
|
|
{
|
|
xml = xml.Substring(1);
|
|
}
|
|
|
|
xml = xml.Replace("�", "");
|
|
xml = xml.Replace("\x00", "");
|
|
|
|
XmlSerializer serializer = new XmlSerializer(typeof(AuthorityData));
|
|
|
|
MemoryStream stream = new MemoryStream();
|
|
byte[] xmlBytes = Encoding.UTF8.GetBytes(xml);
|
|
stream.Write(xmlBytes, 0, xmlBytes.Length);
|
|
|
|
stream.Position = 0;
|
|
AuthorityData result = (AuthorityData)serializer.Deserialize(stream);
|
|
stream.Close();
|
|
|
|
return result;
|
|
}
|
|
}
|
|
} |