Exemplo n.º 1
0
        public UserEditModel UserGet(long? id)
        {
            if (id == null)
                id = 0;
            try
            {
                UserEditModel model = new UserEditModel();
                var filteredQuery = SecurityProvider.GetUserList().Where(a => a.Id == id)
                    .Select(a => new UserEditModel()
                    {
                        Id = a.Id,
                        UserName = a.UserName,
                        FirstName = a.FirstName,
                        Surname = a.Surname,
                        Email = a.Email,
                        IsAdmin = a.IsSystemAdmin

                    });

                 model = filteredQuery.Single();

                model.RoleList = SecurityProvider.GetRoles().Select(a => new UserRoleModel()
                {
                    RoleId = a.Id,
                    RoleName = a.RoleName,
                    Selected = false
                }).ToList();

                var selectedRoles = SecurityProvider.GetUserList().Where(a => a.Id == id)
                                                    .Select(a => a.Roles)
                                                    .Single()
                                                    .Select(a => a.Id).ToArray();

                foreach (var itm in model.RoleList.Where(a => selectedRoles.Contains(a.RoleId)).ToList())
                {
                    itm.Selected = true;
                }

                return model;
            }
            catch (SecurityException e)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e.Message));
            }
        }
Exemplo n.º 2
0
        public UserEditModel UserSave(UserEditModel model)
        {
            try
            {

                if (!ModelState.IsValid)
                    throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Error details"));

                List<long> roles = new List<long>();
                if (model.RoleList != null)
                    roles = model.RoleList.Select(a => a.RoleId).ToList();

                var UserRole = SecurityProvider.SaveUser(model.Id, model.UserName, model.Title, model.FirstName, model.Surname, model.Password, model.Email, roles);

                var user = new UserEditModel()
                {
                    Id = UserRole.Id,
                    UserName = UserRole.UserName,
                    FirstName = UserRole.FirstName,
                    Surname = UserRole.Surname,
                    Email = UserRole.Email,
                    RoleList = UserRole.Roles.Select(a => new UserRoleModel()
                    {
                        RoleId = a.Id,
                        RoleName = a.RoleName
                    }).ToList()
                };

                return user;
            }
            catch (SecurityException e)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e.Message));
            }
        }