Beispiel #1
0
        public JsonResult Delete(string model)
        {
            ApplicationUser appUser = new ApplicationUser();

            try
            {
                UserManagementViewModel user = JsonConvert.DeserializeObject <UserManagementViewModel>(model);
                var userToDelete             = _userManager.Users.Where(u => u.Id == user.UserId).First();
                if (userToDelete != null)
                {
                    IdentityResult result = _userManager.Delete(userToDelete);
                    if (result == IdentityResult.Success)
                    {
                        Response.StatusCode = (int)System.Net.HttpStatusCode.OK;
                        return(Json("success", JsonRequestBehavior.AllowGet));
                    }
                }
                throw new System.Exception("User does not exist.");
            }
            catch (Exception ex)
            {
                // assemble custom error for kendo CRID operation
                Response.StatusCode = (int)System.Net.HttpStatusCode.OK; // custom error return 200 code
                var exception = new                                      // custom response body indicated by the 'errors' field
                {
                    errors = string.Format("Delete {0} fails. {1}", appUser.UserName, ex.Message)
                };
                return(Json(exception, JsonRequestBehavior.AllowGet));
            }
        }
Beispiel #2
0
        public JsonResult Update(string model)
        {
            string typeName = string.Empty;
            string reason   = string.Empty;
            string message  = string.Empty;

            try
            {
                Lookup item = JsonConvert.DeserializeObject <Lookup>(model);
                typeName = item.Type;
                LookupProvider provider = new LookupProvider(_dbContext);

                // check to make sure the lookup name is not used anywhere
                LookupType type;
                if (Enum.TryParse(typeName, out type))
                {
                    // make sure the original one is not in used
                    Lookup oldItem = provider.Retrieve(item.Id);
                    if (provider.InUse(type, oldItem.Name)) // old lookup name is in used; can't change it
                    {
                        reason  = "Conflict";
                        message = string.Format("Cannot change {0} type of '{1}' that is still in used.", typeName, oldItem.Name);
                    }
                    else if (provider.Exist(type, item.Name) || provider.InUse(type, item.Name)) // new lookup name is in used; can't change it
                    {
                        reason  = "Conflict";
                        message = InUseMessage(typeName, item.Name);
                    }
                    else
                    {
                        provider.Update(item.Id, item);
                        provider.Commit();
                        return(Json(item, JsonRequestBehavior.AllowGet));
                    }
                }
                else
                {
                    reason  = "Bad Type";
                    message = TypeNotFound(typeName);
                }
            }
            catch (Exception ex)
            {
                reason  = "Server Error";
                message = ex.Message + " " + ex.InnerException.Message;
            }

            // assemble custom error for kendo CRUD operation
            Response.StatusCode = (int)System.Net.HttpStatusCode.OK; // custom error return 200 code
            var exception = new                                      // custom response body indicated by the 'errors' field
            {
                errors = CrudError("Update", typeName, reason, message)
            };

            return(Json(exception, JsonRequestBehavior.AllowGet));
        }
Beispiel #3
0
        public JsonResult Delete(string model)
        {
            string typeName = string.Empty;
            string reason   = string.Empty;
            string message  = string.Empty;

            try
            {
                Lookup item = JsonConvert.DeserializeObject <Lookup>(model);
                typeName = item.Type;
                LookupProvider provider = new LookupProvider(_dbContext);

                // check to make sure the lookup exists and is not used anywhere
                LookupType type;
                if (Enum.TryParse(typeName, out type))
                {
                    if (provider.InUse(type, item.Name))
                    {
                        reason  = "Conflict";
                        message = InUseMessage(typeName, item.Name);
                    }
                    else
                    {
                        provider.Delete(item.Id);
                        provider.Commit();
                        return(Json(string.Empty, JsonRequestBehavior.AllowGet));
                    }
                }
                else
                {
                    reason  = "Bad Type";
                    message = TypeNotFound(typeName);
                }
            }
            catch (Exception ex)
            {
                reason  = "Server Error";
                message = ex.Message + " " + ex.InnerException.Message;
            }

            // assemble custom error for kendo CRID operation
            Response.StatusCode = (int)System.Net.HttpStatusCode.OK; // custom error return 200 code
            var exception = new                                      // custom response body indicated by the 'errors' field
            {
                errors = CrudError("Delete", typeName, reason, message)
            };

            return(Json(exception, JsonRequestBehavior.AllowGet));
        }
        public JsonResult Update(string model)
        {
            string userName = string.Empty;

            try
            {
                UserRoleManagementViewModel userRole = JsonConvert.DeserializeObject <UserRoleManagementViewModel>(model);
                var user = _userManager.Users.Where(u => u.Id == userRole.UserId).First();
                if (user != null)
                {
                    userName = user.UserName;
                    var            oldRoles = _userManager.GetRoles(user.Id).ToArray();
                    IdentityResult result   = _userManager.RemoveFromRoles(user.Id, oldRoles);
                    if (result == IdentityResult.Success)
                    {
                        var newRoles = userRole.UserRoles.Select(r => r.Text).ToArray();
                        result = _userManager.AddToRoles(user.Id, newRoles);
                        if (result == IdentityResult.Success)
                        {
                            string message = string.Format("Role '{0}' assigned to user '{1}'.", string.Join(", ", newRoles), userName);
                            DojoLogger.Info(message, typeof(UserRoleManagerController));

                            Response.StatusCode = (int)System.Net.HttpStatusCode.OK;
                            return(Json(userRole, JsonRequestBehavior.AllowGet));
                        }
                    }
                    throw new System.Exception("Remove/Add user role from DB fails.");
                }
                throw new System.Exception(string.Format("User does not exist for user ID = '{0}'.", userRole.UserId));
            }
            catch (Exception ex)
            {
                string message = string.Format("Update user role for user '{0}' fails. {1}", userName, ex.Message);
                DojoLogger.Info(message, typeof(UserRoleManagerController));

                // assemble custom error for kendo CRID operation
                Response.StatusCode = (int)System.Net.HttpStatusCode.OK; // custom error return 200 code
                var exception = new                                      // custom response body indicated by the 'errors' field
                {
                    errors = message
                };
                return(Json(exception, JsonRequestBehavior.AllowGet));
            }
        }