public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            if (providerUserKey is Guid)
            {
            }
            else
            {
                return(null);
            }

            using (CodeWickContext Context = new CodeWickContext()) {
                User User = null;
                User = Context.Users.Find(providerUserKey);
                if (User != null)
                {
                    if (userIsOnline)
                    {
                        User.LastActivityDate = DateTime.UtcNow;
                        Context.SaveChanges();
                    }
                    return(new MembershipUser(Membership.Provider.Name, User.Username, User.UserId, User.Email, null, null, User.IsApproved, User.IsLockedOut, User.CreateDate.Value, User.LastLoginDate.Value, User.LastActivityDate.Value, User.LastPasswordChangedDate.Value, User.LastLockoutDate.Value));
                }
                else
                {
                    return(null);
                }
            }
        }
Пример #2
0
        public ActionResult _RoleUsers(Guid id, List <string> available_list, List <string> relationship_list)
        {
            if (id != null)
            {
                using (CodeWickContext context = new CodeWickContext()) {
                    // Get Role
                    Role role = context.Roles.SingleOrDefault(tbl => tbl.RoleId == id);

                    // Remove users from the Role if they are in the available_list
                    if (available_list != null)
                    {
                        foreach (string available in available_list)
                        {
                            Guid userId = Guid.Parse(available);
                            User user   = context.Users.FirstOrDefault(tbl => tbl.UserId == userId);
                            user.Roles.Remove(role);
                            context.SaveChanges();
                        }
                    }

                    // Add Users to Role if they are in the relationship_list
                    if (relationship_list != null)
                    {
                        foreach (string relationship in relationship_list)
                        {
                            Guid userId = Guid.Parse(relationship);
                            User user   = context.Users.FirstOrDefault(tbl => tbl.UserId == userId);
                            user.Roles.Add(role);
                            context.SaveChanges();
                        }
                    }
                }
            }
            return(View(GetUsers(id)));
        }
Пример #3
0
 public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
 {
     if (string.IsNullOrEmpty(roleName))
     {
         return(false);
     }
     using (CodeWickContext Context = new CodeWickContext()) {
         Role Role = null;
         Role = Context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
         if (Role == null)
         {
             return(false);
         }
         if (throwOnPopulatedRole)
         {
             if (Role.Users.Any())
             {
                 return(false);
             }
         }
         else
         {
             Role.Users.Clear();
         }
         Context.Roles.Remove(Role);
         Context.SaveChanges();
         return(true);
     }
 }
Пример #4
0
 public override bool IsUserInRole(string username, string roleName)
 {
     if (string.IsNullOrEmpty(username))
     {
         return(false);
     }
     if (string.IsNullOrEmpty(roleName))
     {
         return(false);
     }
     using (CodeWickContext Context = new CodeWickContext()) {
         User User = null;
         User = Context.Users.FirstOrDefault(Usr => Usr.Username == username);
         if (User == null)
         {
             return(false);
         }
         Role Role = Context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
         if (Role == null)
         {
             return(false);
         }
         return(User.Roles.Contains(Role));
     }
 }
 public override bool ValidateUser(string username, string password)
 {
     if (string.IsNullOrEmpty(username))
     {
         return(false);
     }
     if (string.IsNullOrEmpty(password))
     {
         return(false);
     }
     using (CodeWickContext Context = new CodeWickContext()) {
         User User = null;
         User = Context.Users.FirstOrDefault(Usr => Usr.Username == username);
         if (User == null)
         {
             return(false);
         }
         if (!User.IsApproved)
         {
             return(false);
         }
         if (User.IsLockedOut)
         {
             return(false);
         }
         String  HashedPassword        = User.Password;
         Boolean VerificationSucceeded = (HashedPassword != null && Crypto.VerifyHashedPassword(HashedPassword, password));
         if (VerificationSucceeded)
         {
             User.PasswordFailuresSinceLastSuccess = 0;
             User.LastLoginDate    = DateTime.UtcNow;
             User.LastActivityDate = DateTime.UtcNow;
         }
         else
         {
             int Failures = User.PasswordFailuresSinceLastSuccess;
             if (Failures < MaxInvalidPasswordAttempts)
             {
                 User.PasswordFailuresSinceLastSuccess += 1;
                 User.LastPasswordFailureDate           = DateTime.UtcNow;
             }
             else if (Failures >= MaxInvalidPasswordAttempts)
             {
                 User.LastPasswordFailureDate = DateTime.UtcNow;
                 User.LastLockoutDate         = DateTime.UtcNow;
                 User.IsLockedOut             = true;
             }
         }
         Context.SaveChanges();
         if (VerificationSucceeded)
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
        public override int GetNumberOfUsersOnline()
        {
            DateTime DateActive = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(Convert.ToDouble(Membership.UserIsOnlineTimeWindow)));

            using (CodeWickContext Context = new CodeWickContext()) {
                return(Context.Users.Where(Usr => Usr.LastActivityDate > DateActive).Count());
            }
        }
 public override bool ChangePassword(string username, string oldPassword, string newPassword)
 {
     if (string.IsNullOrEmpty(username))
     {
         return(false);
     }
     if (string.IsNullOrEmpty(oldPassword))
     {
         return(false);
     }
     if (string.IsNullOrEmpty(newPassword))
     {
         return(false);
     }
     using (CodeWickContext Context = new CodeWickContext()) {
         User User = null;
         User = Context.Users.FirstOrDefault(Usr => Usr.Username == username);
         if (User == null)
         {
             return(false);
         }
         String  HashedPassword        = User.Password;
         Boolean VerificationSucceeded = (HashedPassword != null && Crypto.VerifyHashedPassword(HashedPassword, oldPassword));
         if (VerificationSucceeded)
         {
             User.PasswordFailuresSinceLastSuccess = 0;
         }
         else
         {
             int Failures = User.PasswordFailuresSinceLastSuccess;
             if (Failures < MaxInvalidPasswordAttempts)
             {
                 User.PasswordFailuresSinceLastSuccess += 1;
                 User.LastPasswordFailureDate           = DateTime.UtcNow;
             }
             else if (Failures >= MaxInvalidPasswordAttempts)
             {
                 User.LastPasswordFailureDate = DateTime.UtcNow;
                 User.LastLockoutDate         = DateTime.UtcNow;
                 User.IsLockedOut             = true;
             }
             Context.SaveChanges();
             return(false);
         }
         String NewHashedPassword = Crypto.HashPassword(newPassword);
         if (NewHashedPassword.Length > 128)
         {
             return(false);
         }
         User.Password = NewHashedPassword;
         User.LastPasswordChangedDate = DateTime.UtcNow;
         Context.SaveChanges();
         return(true);
     }
 }
Пример #8
0
 public void LogMessage(string location, string message)
 {
     try {
         using (CodeWickContext context = new CodeWickContext()) {
             LogMessage logMessage = new LogMessage();
             logMessage.Location = location;
             logMessage.Message  = message;
             logMessage.Time     = DateTime.Now;
             context.LogMessages.Add(logMessage);
             context.SaveChanges();
         }
     } catch (Exception ex) { LogToWindowsEventViewer(ex); }
 }
Пример #9
0
        public bool SessionSettingSet()
        {
            bool rtn = false;

            try {
                using (CodeWickContext context = new CodeWickContext()) {
                    Setting setting = context.Settings.First();
                    Session["setting"] = setting;

                    rtn = true;
                }
            } catch (Exception ex) { using (LogHelper lh = new LogHelper()) lh.LogException(ex); }
            return(rtn);
        }
Пример #10
0
        public bool SessionThemeSet(long themeId)
        {
            bool rtn = false;

            try {
                using (CodeWickContext context = new CodeWickContext()) {
                    Theme theme = context.Themes.First(x => x.ThemeId == themeId);
                    Session["theme"] = theme;

                    rtn = true;
                }
            } catch (Exception ex) { using (LogHelper lh = new LogHelper()) lh.LogException(ex); }
            return(rtn);
        }
        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            MembershipUserCollection MembershipUsers = new MembershipUserCollection();

            using (CodeWickContext Context = new CodeWickContext()) {
                totalRecords = Context.Users.Count();
                IQueryable <User> Users = Context.Users.OrderBy(Usrn => Usrn.Username).Skip(pageIndex * pageSize).Take(pageSize);
                foreach (User user in Users)
                {
                    MembershipUsers.Add(new MembershipUser(Membership.Provider.Name, user.Username, user.UserId, user.Email, null, null, user.IsApproved, user.IsLockedOut, user.CreateDate.Value, user.LastLoginDate.Value, user.LastActivityDate.Value, user.LastPasswordChangedDate.Value, user.LastLockoutDate.Value));
                }
            }
            return(MembershipUsers);
        }
Пример #12
0
        public ActionResult _NewsTicker()
        {
            List <Content> content = new List <Content>();

            try {
                using (CodeWickContext context = new CodeWickContext()) {
                    // content = context.Contents.Where(tbl => tbl.NewsTicker == true).OrderBy(tbl => tbl.NewsTickerOrder).ToList();
                }
            } catch (Exception ex) {
                LogHelper log = new LogHelper();
                log.LogException(ex);
            }
            return(PartialView(content));
        }
Пример #13
0
        public ActionResult _Content(string id)
        {
            Content content = new Content();

            try {
                using (CodeWickContext context = new CodeWickContext()) {
                    content = context.Contents.FirstOrDefault(tbl => tbl.SEOURL.ToLower() == id.ToLower());
                }
            } catch (Exception ex) {
                LogHelper log = new LogHelper();
                log.LogException(ex);
            }
            return(PartialView(content));
        }
Пример #14
0
        // GET: /Site/Content
        public virtual new ActionResult Content(string id = "")
        {
            Content content = new Content();

            try {
                using (CodeWickContext context = new CodeWickContext()) {
                    content = context.Contents.First(tbl => tbl.SEOURL == id);
                }
            } catch (Exception ex) {
                LogHelper log = new LogHelper();
                log.LogException(ex);
            }

            return(View(content));
        }
Пример #15
0
        // GET: /Site/Category
        public ActionResult Category(string id = "")
        {
            Category Category = new Category();

            try {
                using (CodeWickContext context = new CodeWickContext()) {
                    Category = context.Categories.First(tbl => tbl.SEOURL == id);
                }
            } catch (Exception ex) {
                LogHelper log = new LogHelper();
                log.LogException(ex);
            }

            return(View(Category));
        }
 public override string GetUserNameByEmail(string email)
 {
     using (CodeWickContext Context = new CodeWickContext()) {
         User User = null;
         User = Context.Users.FirstOrDefault(Usr => Usr.Email == email);
         if (User != null)
         {
             return(User.Username);
         }
         else
         {
             return(string.Empty);
         }
     }
 }
Пример #17
0
        public string BuildContent(Category Category)
        {
            string navLinks = "";

            try {
                using (CodeWickContext context = new CodeWickContext()) {
                    List <Content> Contents = context.Contents.Where(tbl => tbl.CategoryId == Category.CategoryId).OrderBy(tbl => tbl.Title).ToList();

                    foreach (Content Content in Contents)
                    {
                        navLinks += "<ul><li><a href=\"" + Request.ApplicationPath + "Site/Content/" + Content.SEOURL + "\">" + Content.Title + "</a></li></ul>";
                    }
                }
            } catch (Exception ex) { LogHelper log = new LogHelper(); log.LogException(ex); }
            return(navLinks);
        }
Пример #18
0
        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            if (string.IsNullOrEmpty(roleName))
            {
                return(null);
            }

            if (string.IsNullOrEmpty(usernameToMatch))
            {
                return(null);
            }

            using (CodeWickContext Context = new CodeWickContext()) {
                return((from Rl in Context.Roles from Usr in Rl.Users where Rl.RoleName == roleName && Usr.Username.Contains(usernameToMatch) select Usr.Username).ToArray());
            }
        }
Пример #19
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            try {
                using (CodeWickContext context = new CodeWickContext()) {
                    Setting       setting = context.Settings.Single();
                    SessionHelper sh      = new SessionHelper();

                    if (sh.SessionSettingSet())              // Set Settings in to Session
                    {
                        sh.SessionThemeSet(setting.ThemeId); // Set Themes in to Session
                        sh.SessionUserSet();
                    }
                }
            } catch (Exception ex) { using (LogHelper lh = new LogHelper()) lh.LogException(ex); }

            base.OnActionExecuting(filterContext);
        }
 public override bool UnlockUser(string userName)
 {
     using (CodeWickContext Context = new CodeWickContext()) {
         User User = null;
         User = Context.Users.FirstOrDefault(Usr => Usr.Username == userName);
         if (User != null)
         {
             User.IsLockedOut = false;
             User.PasswordFailuresSinceLastSuccess = 0;
             Context.SaveChanges();
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
Пример #21
0
 public override void AddUsersToRoles(string[] usernames, string[] roleNames)
 {
     using (CodeWickContext Context = new CodeWickContext()) {
         List <User> Users = Context.Users.Where(Usr => usernames.Contains(Usr.Username)).ToList();
         List <Role> Roles = Context.Roles.Where(Rl => roleNames.Contains(Rl.RoleName)).ToList();
         foreach (User user in Users)
         {
             foreach (Role role in Roles)
             {
                 if (!user.Roles.Contains(role))
                 {
                     user.Roles.Add(role);
                 }
             }
         }
         Context.SaveChanges();
     }
 }
Пример #22
0
 public void LogException(Exception ex)
 {
     try {
         using (CodeWickContext context = new CodeWickContext()) {
             LogException logException = new LogException();
             logException.Source     = ex.Source;
             logException.StackTrace = ex.StackTrace;
             logException.Message    = ex.Message;
             logException.ModuleName = ex.TargetSite.Module.Name;
             logException.Exception  = ex.ToString();
             logException.Time       = DateTime.Now;
             context.LogExceptions.Add(logException);
             context.SaveChanges();
         }
     } catch {
         LogToWindowsEventViewer(ex);
     }
 }
Пример #23
0
 public override string[] GetRolesForUser(string username)
 {
     if (string.IsNullOrEmpty(username))
     {
         return(null);
     }
     using (CodeWickContext Context = new CodeWickContext()) {
         User User = null;
         User = Context.Users.FirstOrDefault(Usr => Usr.Username == username);
         if (User != null)
         {
             return(User.Roles.Select(Rl => Rl.RoleName).ToArray());
         }
         else
         {
             return(null);
         }
     }
 }
Пример #24
0
 public override string[] GetUsersInRole(string roleName)
 {
     if (string.IsNullOrEmpty(roleName))
     {
         return(null);
     }
     using (CodeWickContext Context = new CodeWickContext()) {
         Role Role = null;
         Role = Context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
         if (Role != null)
         {
             return(Role.Users.Select(Usr => Usr.Username).ToArray());
         }
         else
         {
             return(null);
         }
     }
 }
Пример #25
0
 public override bool RoleExists(string roleName)
 {
     if (string.IsNullOrEmpty(roleName))
     {
         return(false);
     }
     using (CodeWickContext Context = new CodeWickContext()) {
         Role Role = null;
         Role = Context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
         if (Role != null)
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
Пример #26
0
 public override void CreateRole(string roleName)
 {
     if (!string.IsNullOrEmpty(roleName))
     {
         using (CodeWickContext Context = new CodeWickContext()) {
             Role Role = null;
             Role = Context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
             if (Role == null)
             {
                 Role NewRole = new Role {
                     RoleId   = Guid.NewGuid(),
                     RoleName = roleName
                 };
                 Context.Roles.Add(NewRole);
                 Context.SaveChanges();
             }
         }
     }
 }
Пример #27
0
        public RoleUsers_ViewModel GetUsers(Guid id)
        {
            RoleUsers_ViewModel Model = new RoleUsers_ViewModel();

            if (id != null)
            {
                using (CodeWickContext context = new CodeWickContext()) {
                    Model.Role           = context.Roles.SingleOrDefault(tbl => tbl.RoleId == id);
                    Model.UsersInRole    = Model.Role.Users.ToList();
                    Model.UsersAvailable = context.Users.ToList();

                    foreach (User user in Model.UsersInRole)
                    {
                        Model.UsersAvailable.Remove(user);
                    }
                }
            }

            return(Model);
        }
Пример #28
0
        public bool SessionUserSet()
        {
            bool rtn = false;

            try {
                // Get User information from database
                Guid userID = new Guid(Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString());

                if (userID != null)
                {
                    using (CodeWickContext context = new CodeWickContext()) {
                        User user = context.Users.First(tbl => tbl.UserId == userID);
                        Session["user"] = user; // Place User information in to session
                        rtn             = true;
                    }
                }
                rtn = true;
            } catch (Exception ex) { using (LogHelper lh = new LogHelper()) lh.LogException(ex); }
            return(rtn);
        }
 public override bool DeleteUser(string username, bool deleteAllRelatedData)
 {
     if (string.IsNullOrEmpty(username))
     {
         return(false);
     }
     using (CodeWickContext Context = new CodeWickContext()) {
         User User = null;
         User = Context.Users.FirstOrDefault(Usr => Usr.Username == username);
         if (User != null)
         {
             Context.Users.Remove(User);
             Context.SaveChanges();
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
Пример #30
0
        public ActionResult _Navigation(string id = "")
        {
            try {
                using (CodeWickContext context = new CodeWickContext()) {
                    // Check SEOURL for match in Categories first
                    Category Category = context.Categories.FirstOrDefault(tbl => tbl.SEOURL == id);

                    // Match not found
                    if (Category != null)
                    {
                        ViewBag.NavLinks = BuildCategory(FindParentCategory(Category));
                    }
                    else     // Check SEOURL for match in Content
                    {
                        Content Content = context.Contents.FirstOrDefault(tbl => tbl.SEOURL == id);
                        Category         = context.Categories.FirstOrDefault(tbl => tbl.CategoryId == Content.CategoryId);
                        ViewBag.NavLinks = BuildCategory(FindParentCategory(Category));
                    }
                }
            } catch (Exception ex) { LogHelper log = new LogHelper(); log.LogException(ex); }
            return(PartialView());
        }