using System; using System.Collections.Generic; using System.Net; using System.Web; using Swarmops.Basic.Types; using Swarmops.Basic.Types.System; using Swarmops.Database; using Swarmops.Logic.Swarm; namespace Swarmops.Logic.Support { // ReSharper disable InconsistentNaming public class PWLog // ReSharper restore InconsistentNaming { private static string hostIp = ""; //Used in batch programs public static void Write (DateTime dateTimeUtc, int actingPersonId, PWLogItem affectedItem, int affectedItemId, PWLogAction actionType, string actionDescription, string comment, string changedField, string valueBefore, string valueAfter) { SwarmDb.GetDatabaseForWriting().CreatePWLogEntry (dateTimeUtc, actingPersonId, affectedItem.ToString(), affectedItemId, actionType.ToString(), actionDescription, changedField, valueBefore, valueAfter, comment, GetCurrentIp()); } public static void Write (DateTime dateTimeUtc, Person actingPerson, PWLogItem affectedItem, int affectedItemId, PWLogAction actionType, string actionDescription, string comment, string changedField, string valueBefore, string valueAfter) { Write (dateTimeUtc, actingPerson.Identity, affectedItem, affectedItemId, actionType, actionDescription, comment, changedField, valueBefore, valueAfter); } public static void Write (Person actingPerson, PWLogItem affectedItem, int affectedItemId, PWLogAction actionType, string actionDescription, string comment, string changedField, string valueBefore, string valueAfter) { Write (DateTime.UtcNow, actingPerson.Identity, affectedItem, affectedItemId, actionType, actionDescription, comment, changedField, valueBefore, valueAfter); } public static void Write (Person actingPerson, PWLogItem affectedItem, int affectedItemId, PWLogAction actionType, string actionDescription, string comment) { Write (DateTime.UtcNow, actingPerson.Identity, affectedItem, affectedItemId, actionType, actionDescription, comment, string.Empty, string.Empty, string.Empty); } public static void Write (int actingPersonId, PWLogItem affectedItem, int affectedItemId, PWLogAction actionType, string actionDescription, string comment) { if (actingPersonId > 0 && comment == "") { try { comment = "Action done by " + Person.FromIdentity (actingPersonId).Name; } catch { } } Write (DateTime.UtcNow, actingPersonId, affectedItem, affectedItemId, actionType, actionDescription, comment, string.Empty, string.Empty, string.Empty); } public static void Write (PWLogItem affectedItem, int affectedItemId, PWLogAction actionType, string actionDescription, string comment) { Write (DateTime.UtcNow, 0, affectedItem, affectedItemId, actionType, actionDescription, comment, string.Empty, string.Empty, string.Empty); } private static string GetCurrentIp() { try { //Get IP from current web request, if available return HttpContext.Current.Request.UserHostAddress; } catch { //Probably in a batch program, if (hostIp == "") { try { hostIp = "n/a"; string myHostName = Dns.GetHostName(); hostIp = myHostName; IPHostEntry ipEntry = Dns.GetHostEntry (myHostName); IPAddress[] addr = ipEntry.AddressList; hostIp = addr[0].ToString(); } catch { } } return hostIp; } } public static DateTime CheckLatest (PWLogItem affectedItemType, int affectedItemId, PWLogAction actionType) { return SwarmDb.GetDatabaseForReading() .CheckLogEntry (affectedItemType.ToString(), affectedItemId, actionType.ToString()); } public static BasicPWLog[] GetLatestEvents (PWLogItem affectedItemType, DateTime beforeDate, int[] affectedIds, PWLogAction[] actionTypes) { List actionTypeList = new List(); foreach (PWLogAction at in actionTypes) actionTypeList.Add (at.ToString()); return SwarmDb.GetDatabaseForReading() .GetLatestEvents (affectedItemType.ToString(), beforeDate, affectedIds, actionTypeList.ToArray()); } } public enum PWLogAction { /// /// Undefined. Do not use. /// Undefined = 0, /// /// A new member has joined on his/her own accord. /// MemberJoin, /// /// A new member was added by an officer. /// MemberAdd, /// /// A new membership was added to an existing member. /// MembershipAdd, /// /// A membership was removed. /// MembershipRemove, /// /// A membership was transferred to another org. /// MembershipTransfer, /// /// A member has been removed from the roster. /// MemberLost, /// /// An activist has joined. /// ActivistJoin, /// /// An activist was lost. /// ActivistLost, /// /// Personal data was changed. /// PersonFieldChange, /// /// Part of systems tests. Do not use. /// SystemTest, /// /// A member was reminded to renew. /// MembershipRenewReminder, /// /// A membership was renewed. /// MembershipRenewed, /// /// A role (lead, admin, etc.) was assigned. /// RoleAssigned, /// /// A role was removed. /// RoleDeleted, /// /// Person created (usually for debug purposes). /// PersonCreated, /// /// Mail account changes /// MailAccountChanged, /// /// Changes in External Identities /// ExtAccountChanged, /// /// A membership is requested to be renewed. /// MembershipRenewalRequest, /// /// Logged info about a failure /// Failure, /// /// SMS message Handled /// SMSHandled, /// /// Starting Impersonation of other user /// StartImpersonation } public enum PWLogItem { None = 0, Person, MailAccount, ExtAccount } }