Пример #1
0
        public void UpdateRole(webpages_Roles roleInput)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var repo = new Durandal2UserMaintContext();
                    var role =
                        (from v in repo.webpages_Roles where v.RoleId == roleInput.RoleId select v).FirstOrDefault();

                    if (role != null)
                    {
                        role.RoleName = roleInput.RoleName;
                        repo.SaveChanges();
                    }
                    else
                    {
                        throw new Exception("Role to update not found");
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
            else
            {
                string errors = ModelState.Values.SelectMany(modelState => modelState.Errors).Aggregate(string.Empty, (current, error) => current + (error.ErrorMessage + Environment.NewLine));
                throw new Exception(errors);
            }
        }
Пример #2
0
        public void AddRoleToUser(UserRoleEditModel userRole)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    //I tried using the Roles.AddUserToRole but I was getting a foreign key error. So I decided this way.

                    if (Roles.IsUserInRole(userRole.UserName, userRole.RoleName))
                    {
                        throw new Exception("User is already assigned that role");
                    }
                    var repo = new Durandal2UserMaintContext();
                    var user =
                        (from v in repo.UserProfiles select v).FirstOrDefault(x => x.UserName == userRole.UserName);
                    if (user == null)
                    {
                        throw new Exception("User to update not found");
                    }
                    else
                    {
                        var role =
                            (from v in repo.webpages_Roles select v).FirstOrDefault(x => x.RoleName == userRole.RoleName);
                        if (role == null)
                        {
                            throw new Exception("Role to use not found");
                        }
                        else
                        {
                            user.webpages_Roles.Add(role);
                            repo.SaveChanges();
                        }
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
            else
            {
                string errors = ModelState.Values.SelectMany(modelState => modelState.Errors).Aggregate(string.Empty, (current, error) => current + (error.ErrorMessage + Environment.NewLine));
                throw new Exception(errors);
            }
        }
Пример #3
0
        public void UpdateUserInfo(UserProfile userProfile)
        {
            if (ModelState.IsValid)
            {
                var repo = new Durandal2UserMaintContext();

                var user = (from v in repo.UserProfiles select v).FirstOrDefault(x => x.UserName == userProfile.UserName);
                if (user != null)
                {
                    user.UserEmail = userProfile.UserEmail;
                    user.IsActive  = userProfile.IsActive;
                    repo.SaveChanges();
                }
                else
                {
                    throw new Exception("User to update not found");
                }
            }
            else
            {
                string errors = ModelState.Values.SelectMany(modelState => modelState.Errors).Aggregate(string.Empty, (current, error) => current + (error.ErrorMessage + Environment.NewLine));
                throw new Exception(errors);
            }
        }