Example #1
0
        /// <summary>
        /// Gets a user using the specified Email Address and Password and populates necessary roles
        /// </summary>
        /// <param name="email">Email Address of the user to be fetched</param>
        /// <param name="password">Password of the user to be fetched</param>
        /// <returns></returns>
        public UserModel Login(string email, string password, bool resetPin = false)
        {
            if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
            {
                return(null);
            }

            UserModel model = new UserModel();

            password = GetSha1Md5String(password);

            model = (from u in context.Users
                     where
                     (
                         u.Email.Trim() == email.Trim() &&
                         u.Password == password &&
                         u.Status == ( int )Status.Active
                     )
                     select new UserModel()
            {
                Id = u.Id,
                Pin = u.Pin,
                Cell = u.Cell,
                Name = u.Name,
                Email = u.Email,
                Surname = u.Surname,
                CreatedOn = u.CreatedOn,
                Status = ( Status )u.Status,
                RoleType = ( RoleType )u.Type,
                DisplayName = u.Name + " " + u.Surname,

                NiceCreatedOn = u.CreatedOn,
                IsAdmin = u.UserRoles.Any(ur => ur.Role.Administration),

                Roles = u.UserRoles.Select(ur => ur.Role)
                        .OrderByDescending(r => r.Id)
                        .ToList(),
                PSPs = u.PSPUsers.Select(p => p.PSP).ToList(),
                Clients = u.ClientUsers.Select(c => c.Client).ToList(),
                SelfieUrl = context.Images
                            .Where(a => a.ObjectId == u.Id && a.ObjectType == "User" && a.Name.ToLower() == "selfie")
                            .Select(s => SystemConfig.ImagesLocation + "//" + s.Location).FirstOrDefault(),
            }).FirstOrDefault();

            if (model != null)
            {
                // Get roles
                model = this.ConfigRoles(model);

                User user = context.Users.FirstOrDefault(u => u.Id == model.Id);

                if (resetPin)
                {
                    user.Pin = null;
                }

                user.LastLogin = DateTime.Now;

                context.Entry(user).State = EntityState.Modified;
                context.SaveChanges();

                ContextExtensions.CacheUserData(model.Email, model);
            }

            return(model);
        }
        /// <summary>
        /// Creates a new audit log
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="activity"></param>
        /// <param name="newItem"></param>
        /// <param name="oldItem"></param>
        public bool Create <T>(ActivityTypes activity, T newItem, T oldItem = null, int userId = 0) where T : class
        {
            try
            {
                dynamic oldObj = new ExpandoObject(),
                        newObj = new ExpandoObject();

                var oldDic = oldObj as IDictionary <string, object>;
                var newDic = newObj as IDictionary <string, object>;

                if (oldItem != null)
                {
                    var oldprops = oldItem.GetType().GetProperties();

                    foreach (var item in oldprops)
                    {
                        if (
                            (item.PropertyType != null) &&
                            (item.PropertyType == typeof(string) ||
                             item.PropertyType == typeof(int) ||
                             item.PropertyType == typeof(int?) ||
                             item.PropertyType == typeof(decimal) ||
                             item.PropertyType == typeof(decimal? ) ||
                             item.PropertyType == typeof(DateTime) ||
                             item.PropertyType == typeof(DateTime? ) ||
                             item.PropertyType == typeof(double) ||
                             item.PropertyType == typeof(double?) ||
                             item.PropertyType == typeof(TimeSpan) ||
                             item.PropertyType == typeof(TimeSpan? ) ||
                             item.PropertyType == typeof(bool) ||
                             item.PropertyType == typeof(bool?) ||
                             item.PropertyType == typeof(byte) ||
                             item.PropertyType == typeof(byte?))
                            )
                        {
                            oldDic[item.Name] = item.GetValue(oldItem);
                        }
                    }
                }

                var props = newItem.GetType().GetProperties();

                foreach (var item in props)
                {
                    if (
                        (item.PropertyType != null) &&
                        (item.PropertyType == typeof(string) ||
                         item.PropertyType == typeof(int) ||
                         item.PropertyType == typeof(int?) ||
                         item.PropertyType == typeof(decimal) ||
                         item.PropertyType == typeof(decimal? ) ||
                         item.PropertyType == typeof(DateTime) ||
                         item.PropertyType == typeof(DateTime? ) ||
                         item.PropertyType == typeof(double) ||
                         item.PropertyType == typeof(double?) ||
                         item.PropertyType == typeof(TimeSpan) ||
                         item.PropertyType == typeof(TimeSpan? ) ||
                         item.PropertyType == typeof(bool) ||
                         item.PropertyType == typeof(bool?) ||
                         item.PropertyType == typeof(byte) ||
                         item.PropertyType == typeof(byte?))
                        )
                    {
                        newDic[item.Name] = item.GetValue(newItem);
                    }
                }

                string actionTable = newItem.GetType().BaseType.Name;

                if (actionTable.ToLower() == "object")
                {
                    actionTable = newItem.GetType().Name;
                }

                string before = (oldItem != null) ? Newtonsoft.Json.JsonConvert.SerializeObject(oldObj) : string.Empty;
                string after  = (newObj != null) ? Newtonsoft.Json.JsonConvert.SerializeObject(newObj) : string.Empty;

                //if ( before == after ) return false;

                HttpBrowserCapabilities browser = HttpContext.Current.Request.Browser;

                string b = string.Format("Type={1} {0} Name={2} {0} Version={3} {0} Platform={4} {0} Supports JavaScript={5}", Environment.NewLine,
                                         browser.Type, browser.Browser, browser.Version, browser.Platform, browser.EcmaScriptVersion.ToString());

                if (userId == 0)
                {
                    userId = (CurrentUser?.Id > 0) ? CurrentUser.Id : 0;
                }

                AuditLog log = new AuditLog()
                {
                    Browser       = b,
                    UserId        = userId,
                    AfterImage    = after,
                    BeforeImage   = before,
                    Type          = ( int )activity,
                    CreatedOn     = DateTime.Now,
                    ModifiedOn    = DateTime.Now,
                    ActionTable   = actionTable,
                    Comments      = string.Format("Created/Updated a {0}", actionTable),
                    ModifiedBy    = ((!string.IsNullOrEmpty(CurrentUser?.Email)) ? CurrentUser.Email : "System"),
                    IsAjaxRequest = (HttpContext.Current.Request.Headers["X-Requested-With"] == "XMLHttpRequest"),
                    Action        = (HttpContext.Current.Request.RequestContext.RouteData.Values["action"] as string) ?? string.Empty,
                    Controller    = (HttpContext.Current.Request.RequestContext.RouteData.Values["controller"] as string) ?? string.Empty,
                    ObjectId      = ( int )newItem.GetType().GetProperties().FirstOrDefault(x => x.Name == "Id").GetValue(newItem),
                    Parameters    = string.Empty //new JavaScriptSerializer().Serialize( HttpContext.Current.Request.RequestContext.RouteData.Values )
                };

                _context.AuditLogs.Add(log);
                _context.SaveChanges();
            }
            catch (Exception ex)
            {
            }

            return(true);
        }