public SimpleMembershipInitializer()
            {
                Database.SetInitializer<UsersContext>(null);

                try
                {
                    using (var context = new UsersContext())
                    {
                        if (!context.Database.Exists())
                        {
                            // Create the SimpleMembership database without Entity Framework migration schema
                            ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                        }
                    }

                    WebSecurity.InitializeDatabaseConnection("AQRConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
                }
            }
Exemplo n.º 2
0
        public JsonResult ApplyLoginSystemToHumanResourceSystem()
        {
            string result = string.Empty;

            try
            {
                var ctx = new UsersContext();

                foreach (UserProfile user in ctx.UserProfiles)
                {
                    string username = user.UserName;
                    int userId = user.UserId;

                    var queryPersonAccount = unitOfWork.PersonAccountRepository.Get(m => m.Person.Email.Equals(username));
                    if (queryPersonAccount.FirstOrDefault() != null)
                        queryPersonAccount.FirstOrDefault().UserId = userId;
                    else
                    {
                        Person queryPerson = unitOfWork.PersonRepository.Get(m => m.Email.Equals(username)).FirstOrDefault();
                        if (queryPerson != null)
                        {
                            unitOfWork.PersonAccountRepository.Insert(new PersonAccount()
                            {
                                UserId = userId,
                                Person = unitOfWork.PersonRepository.Get(m => m.Email.Equals(username)).FirstOrDefault()
                            });
                        }
                    }
                }

                unitOfWork.Save();
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }

            return Json(result);
        }
Exemplo n.º 3
0
        public string Update(int id, string value, int? rowId, int? columnPosition, int? columnId, string columnName)
        {
            switch (columnPosition)
            {
                case 0:
                    var ctx = new UsersContext();

                    var queryUser = ctx.UserProfiles.Where(m => m.UserName.Equals(value));
                    if (queryUser.FirstOrDefault() == null)
                    {
                        queryUser = ctx.UserProfiles.Where(m => m.UserId == id);
                        queryUser.FirstOrDefault().UserName = value;
                        ctx.SaveChanges();
                    }

                    break;

                case 1:
                    PersonAccount querypa = unitOfWork.PersonAccountRepository.Get(m => m.UserId == id).FirstOrDefault();
                    if (querypa != null)
                    {
                        querypa.Person = unitOfWork.PersonRepository.GetByID(Convert.ToInt32(value.Trim()));
                        querypa.ModifiedDate = System.DateTime.Now;
                        unitOfWork.Save();
                    }
                    else
                    {
                        PersonAccount personAccount = new PersonAccount()
                        {
                            UserId = id,
                            Person = unitOfWork.PersonRepository.GetByID(Convert.ToInt32(value.Trim()))
                        };

                        unitOfWork.PersonAccountRepository.Insert(personAccount);
                        unitOfWork.Save();
                    }

                    break;

                case 2:
                    ctx = new UsersContext();

                    if (value == "true")
                    {
                        if (Roles.FindUsersInRole(RoleNames.view1Role, ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName).FirstOrDefault() == null)
                        {
                            Roles.AddUserToRole(ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName, RoleNames.view1Role);
                        }
                    }

                    if (value == "false")
                    {
                        if (Roles.FindUsersInRole(RoleNames.view1Role, ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName).FirstOrDefault() != null)
                        {
                            Roles.RemoveUserFromRole(ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName, RoleNames.view1Role);
                        }
                    }

                    break;

                case 3:
                    ctx = new UsersContext();

                    if (value == "true")
                    {
                        if (Roles.FindUsersInRole(RoleNames.view2Role, ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName).FirstOrDefault() == null)
                        {
                            Roles.AddUserToRole(ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName, RoleNames.view2Role);
                        }
                    }

                    if (value == "false")
                    {
                        if (Roles.FindUsersInRole(RoleNames.view2Role, ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName).FirstOrDefault() != null)
                        {
                            Roles.RemoveUserFromRole(ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName, RoleNames.view2Role);
                        }
                    }

                    break;

                case 4:
                    ctx = new UsersContext();

                    if (value == "true")
                    {
                        if (Roles.FindUsersInRole(RoleNames.view3Role, ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName).FirstOrDefault() == null)
                        {
                            Roles.AddUserToRole(ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName, RoleNames.view3Role);
                        }
                    }

                    if (value == "false")
                    {
                        if (Roles.FindUsersInRole(RoleNames.view3Role, ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName).FirstOrDefault() != null)
                        {
                            Roles.RemoveUserFromRole(ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName, RoleNames.view3Role);
                        }
                    }

                    break;

                case 5:
                    ctx = new UsersContext();

                    if (value == "true")
                    {
                        if (Roles.FindUsersInRole(RoleNames.editRole, ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName).FirstOrDefault() == null)
                        {
                            Roles.AddUserToRole(ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName, RoleNames.editRole);
                        }
                    }

                    if (value == "false")
                    {
                        if (Roles.FindUsersInRole(RoleNames.editRole, ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName).FirstOrDefault() != null)
                        {
                            Roles.RemoveUserFromRole(ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName, RoleNames.editRole);
                        }
                    }

                    break;

                case 6:
                    ctx = new UsersContext();
                    if (value == "true")
                    {
                        if (Roles.FindUsersInRole(RoleNames.deleteRole, ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName).FirstOrDefault() == null)
                        {
                            Roles.AddUserToRole(ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName, RoleNames.deleteRole);
                        }
                    }

                    if (value == "false")
                    {
                        if (Roles.FindUsersInRole(RoleNames.deleteRole, ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName).FirstOrDefault() != null)
                        {
                            Roles.RemoveUserFromRole(ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName, RoleNames.deleteRole);
                        }
                    }

                    break;

                case 7:
                    ctx = new UsersContext();
                    if (value == "true")
                    {
                        if (Roles.FindUsersInRole(RoleNames.adminRole, ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName).FirstOrDefault() == null)
                        {
                            Roles.AddUserToRole(ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName, RoleNames.adminRole);
                        }
                    }

                    if (value == "false")
                    {
                        if (Roles.FindUsersInRole(RoleNames.adminRole, ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName).FirstOrDefault() != null)
                        {
                            Roles.RemoveUserFromRole(ctx.UserProfiles.Where(m => m.UserId == id).FirstOrDefault().UserName, RoleNames.adminRole);
                        }
                    }

                    break;

                default:
                    break;
            }

            return value;
        }
Exemplo n.º 4
0
        public ActionResult GetList(JQueryDataTableParamModel param)
        {
            var ctx = new UsersContext();
            var allUsers = ctx.UserProfiles;

            IEnumerable<UserProfile> filteredUsers;

            if (!string.IsNullOrEmpty(param.sSearch))
            {
                //Used if particulare columns are filtered
                var usernameFilter = Convert.ToString(Request["sSearch_1"]);
                var rolenameFilter = Convert.ToString(Request["sSearch_2"]);
                var personnameFilter = Convert.ToString(Request["sSearch_3"]);

                //Optionally check whether the columns are searchable at all
                var isUsernameSearchable = Convert.ToBoolean(Request["bSearchable_1"]);
                var isRolenameSearchable = Convert.ToBoolean(Request["bSearchable_2"]);
                var isPersonnameSearchable = Convert.ToBoolean(Request["bSearchable_3"]);

                filteredUsers = allUsers
                   .Where(c => isUsernameSearchable && c.UserName != null && c.UserName.ToLower().Contains(param.sSearch.ToLower()));
            }
            else
            {
                filteredUsers = allUsers;
            }

            var isUsernameSortable = Convert.ToBoolean(Request["bSortable_1"]);
            var isRolenameSortable = Convert.ToBoolean(Request["bSortable_2"]);
            var isPersonnameSortable = Convert.ToBoolean(Request["bSortable_3"]);
            var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
            Func<UserProfile, string> orderingFunction = (c => sortColumnIndex == 1 && isUsernameSortable ? c.UserName : "");

            var sortDirection = Request["sSortDir_0"]; // asc or desc
            if (sortDirection == "asc")
                filteredUsers = filteredUsers.OrderBy(orderingFunction);
            else
                filteredUsers = filteredUsers.OrderByDescending(orderingFunction);

            IEnumerable<UserProfile> displayedUsers;

            if (param.iDisplayLength != -1)
                displayedUsers = filteredUsers.Skip(param.iDisplayStart).Take(param.iDisplayLength);
            else
                displayedUsers = filteredUsers;

            var result = from c in displayedUsers
                         select new string[]
                              {
                                  c.UserId.ToString()
                                  ,c.UserName
                                  ,unitOfWork.PersonAccountRepository.Get(m=>m.UserId==c.UserId).FirstOrDefault() !=null ?  unitOfWork.PersonAccountRepository.Get(m=>m.UserId==c.UserId).FirstOrDefault().Person !=null ? unitOfWork.PersonAccountRepository.Get(m=>m.UserId==c.UserId).FirstOrDefault().Person.Lastname + " " + unitOfWork.PersonAccountRepository.Get(m=>m.UserId==c.UserId).FirstOrDefault().Person.Firstname : string.Empty: string.Empty
                                  ,Roles.FindUsersInRole(RoleNames.view1Role,c.UserName).FirstOrDefault()!=null ? "1" : "0"
                                  ,Roles.FindUsersInRole(RoleNames.view2Role,c.UserName).FirstOrDefault()!=null ? "1" : "0"
                                  ,Roles.FindUsersInRole(RoleNames.view3Role,c.UserName).FirstOrDefault()!=null ? "1" : "0"
                                  ,Roles.FindUsersInRole(RoleNames.editRole,ctx.UserProfiles.FirstOrDefault(m=>m.UserId==c.UserId).UserName).FirstOrDefault()!=null ? "1" : "0"
                                  ,Roles.FindUsersInRole(RoleNames.deleteRole,c.UserName).FirstOrDefault()!=null ? "1" : "0"
                                  ,Roles.FindUsersInRole(RoleNames.adminRole,c.UserName).FirstOrDefault()!=null ? "1" : "0"
                              };

            return Json(new
            {
                sEcho = param.sEcho,
                iTotalRecords = allUsers.Count(),
                iTotalDisplayRecords = filteredUsers.Count(),
                aaData = result
            },
                        JsonRequestBehavior.AllowGet);
        }
Exemplo n.º 5
0
        public string GetFirstnameByUserName(string username)
        {
            string result = string.Empty;
            var ctx = new UsersContext();
            var queryUserId = ctx.UserProfiles.Where(m => m.UserName.Equals(username));
            if (queryUserId.FirstOrDefault() != null)
            {
                int userId = queryUserId.FirstOrDefault().UserId;
                var queryUser = unitOfWork.PersonAccountRepository.Get(m => m.UserId == userId);
                if (queryUser.FirstOrDefault() != null)
                {
                    var p = queryUser.FirstOrDefault().Person;
                    if (p != null)
                        result = p.Firstname;
                }
            }

            return result;
        }
Exemplo n.º 6
0
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Insert a new user into the database
                using (UsersContext db = new UsersContext())
                {
                    UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
                    // Check if user already exists
                    if (user == null)
                    {
                        // Insert name into the profile table
                        db.UserProfiles.Add(new UserProfile { UserName = model.UserName });
                        db.SaveChanges();

                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                    }
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
Exemplo n.º 7
0
        public string DeleteByUserId(int id)
        {
            string result = string.Empty;
            try
            {
                var ctx = new UsersContext();

                var queryUser = ctx.UserProfiles.FirstOrDefault(m => m.UserId == id);
                if (queryUser != null)
                {
                    string userName = queryUser.UserName;
                    if (!Roles.IsUserInRole(userName, RoleNames.adminRole))
                    {
                        if (Roles.GetRolesForUser(userName).Count() > 0)
                        {
                            Roles.RemoveUserFromRoles(userName, Roles.GetRolesForUser(userName));
                        }
                        ((SimpleMembershipProvider)Membership.Provider).DeleteAccount(userName); // deletes record from webpages_Membership table
                        ((SimpleMembershipProvider)Membership.Provider).DeleteUser(userName, true); // deletes record from UserProfile table

                        unitOfWork.DataContext.PersonAccounts.RemoveRange(unitOfWork.PersonAccountRepository.Get(m => m.UserId == id));
                        unitOfWork.Save();
                    }
                }
                else
                {
                    unitOfWork.DataContext.PersonAccounts.RemoveRange(unitOfWork.PersonAccountRepository.Get(m => m.UserId == id));
                    unitOfWork.Save();
                }

            }
            catch (Exception ex)
            {
                result = ex.Message;
            }

            return result;
        }
Exemplo n.º 8
0
        public JsonResult ChangeUserName(int userId, string userName)
        {
            string result = string.Empty;
            try
            {
                if (!WebSecurity.UserExists(userName))
                {
                    var ctx = new UsersContext();
                    var queryUser = ctx.UserProfiles.FirstOrDefault(m => m.UserId == userId);

                    if (queryUser != null)
                    {
                        queryUser.UserName = userName;
                        ctx.SaveChanges();
                    }
                    result = "Đổi email truy cập hệ thống thành công.";
                }
                else
                {
                    result = "Email này đã được sử dụng.";
                }

            }
            catch (Exception e)
            {
                result = e.Message;
            }

            return Json(result);
        }