Beispiel #1
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);
        }
Beispiel #2
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;
        }
Beispiel #3
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);
        }