public HttpResponseMessage SavePermission(HttpRequestMessage request, SavePermissionRequest data)
        {
            string connectionstring = "server=DESKTOP-I9HJM7B; Database=DuAnWebApi_Angular; uid=DamNgocSon ; pwd =123 ;";

            if (ModelState.IsValid)
            {
                _permissionService.DeleteAll(data.FunctionId);
                _permissionService.SaveChange();
                Permission permission = null;
                foreach (var item in data.Permissions)
                {
                    permission = new Permission();
                    permission.UpdatePermission(item);
                    permission.FunctionId = data.FunctionId;
                    _permissionService.Add(permission);
                }
                var functions = _functionService.GetAllWithParentID(data.FunctionId);
                if (functions.Any())
                {
                    using (SqlConnection conn = new SqlConnection(connectionstring))
                    {
                        foreach (var item in functions)
                        {
                            conn.Open();
                            SqlCommand sqlCommand = new SqlCommand("DeletePermissionWithFunctionId", conn);
                            sqlCommand.CommandType = CommandType.StoredProcedure;

                            sqlCommand.ExecuteNonQuery();
                            conn.Close();
                            foreach (var p in data.Permissions)
                            {
                                var childPermission = new Permission();
                                childPermission.FunctionId = item.ID;
                                childPermission.RoleId     = p.RoleId;
                                childPermission.CanRead    = p.CanRead;
                                childPermission.CanCreate  = p.CanCreate;
                                childPermission.CanDelete  = p.CanDelete;
                                childPermission.CanUpdate  = p.CanUpdate;
                                _permissionService.Add(childPermission);
                            }
                        }
                    }
                }
                try
                {
                    _permissionService.SaveChange();
                    return(request.CreateResponse(HttpStatusCode.OK, "Lưu quyền thành cống"));
                }
                catch (Exception ex)
                {
                    return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message));
                }
            }
            else
            {
                return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }
Exemplo n.º 2
0
        public HttpResponseMessage SavePermission(HttpRequestMessage request, SavePermissionRequest data)
        {
            if (ModelState.IsValid)
            {
                _permissionService.DeleteAll(data.FunctionId);

                Permission permission = null;
                foreach (var item in data.Permissions)
                {
                    string[] result = JsonConvert.DeserializeObject<string[]>(item.RoleId);
                    foreach (var role in result)
                    {
                        var roleId = AppRoleManager.Roles.Where(x => x.Name == role.ToString()).FirstOrDefault();
                        item.RoleId = roleId.Id;
                        permission = new Permission();
                        permission.UpdatePermission(item);
                        permission.FunctionId = data.FunctionId;
                        _permissionService.Add(permission);
                    }
                }
                var functions = _functionService.GetAllWithParentID(data.FunctionId);
                if (functions.Any())
                {
                    foreach (var item in functions)
                    {
                        _permissionService.DeleteAll(item.ID);

                        foreach (var p in data.Permissions)
                        {
                            var childPermission = new Permission();
                            childPermission.FunctionId = item.ID;
                            childPermission.RoleId = p.RoleId;
                            childPermission.CanRead = p.CanRead;
                            childPermission.CanCreate = p.CanCreate;
                            childPermission.CanDelete = p.CanDelete;
                            childPermission.CanUpdate = p.CanUpdate;
                            _permissionService.Add(childPermission);
                        }
                    }
                }
                try
                {
                    _permissionService.SaveChange();
                    return request.CreateResponse(HttpStatusCode.OK, "Lưu quyền thành cống");
                }
                catch (Exception ex)
                {
                    return request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message);
                }
            }
            else
            {
                return request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
Exemplo n.º 3
0
 public HttpResponseMessage SavePermission(HttpRequestMessage request, SavePermissionRequest data)
 {
     if (ModelState.IsValid)
     {
         _permissionService.DeleteAll(data.FunctionId);
         Permission permission = null;
         foreach (var item in data.Permissions)
         {
             permission = new Permission();
             permission.UpdatePermission(item);
             permission.FunctionId = data.FunctionId;
             _permissionService.Add(permission);
         }
         var functions = _functionService.GetAllWithParentID(data.FunctionId);
         if (functions.Any())
         {
             foreach (var item in functions)
             {
                 if (_permissionService.CheckContaint(item.ID))
                 {
                     _permissionService.DeleteAll(item.ID);
                 }
                 foreach (var p in data.Permissions)
                 {
                     var childPermission = new Permission();
                     childPermission.FunctionId = item.ID;
                     childPermission.RoleId     = p.RoleId;
                     childPermission.CanRead    = p.CanRead;
                     childPermission.CanCreate  = p.CanCreate;
                     childPermission.CanDelete  = p.CanDelete;
                     childPermission.CanUpdate  = p.CanUpdate;
                     _permissionService.Add(childPermission);
                 }
             }
         }
         try
         {
             _permissionService.SaveChange();
             return(request.CreateResponse(HttpStatusCode.OK, "Lưu quyền thành cống"));
         }
         catch (Exception ex)
         {
             return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message));
         }
     }
     else
     {
         return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
     }
 }
Exemplo n.º 4
0
        public HttpResponseMessage SavePermission(HttpRequestMessage request, SavePermissionRequest data)
        {
            Func <HttpResponseMessage> func = () =>
            {
                HttpResponseMessage response = null;
                if (ModelState.IsValid)
                {
                    _permissionService.DeleteAll(data.FunctionId);
                    foreach (var item in data.Permissions)
                    {
                        Permission permission = new Permission();
                        permission.UpdatePermission(item);
                        permission.FunctionId = data.FunctionId;
                        _permissionService.Add(permission);
                    }
                    var functions = _functionService.GetAllWithParentID(data.FunctionId);
                    if (functions.Any())
                    {
                        foreach (var item in functions)
                        {
                            _permissionService.DeleteAll(item.ID);
                            foreach (var per in data.Permissions)
                            {
                                var permission = new Permission()
                                {
                                    FunctionId = item.ID,
                                    RoleId     = per.RoleId,
                                    CanCreate  = per.CanCreate,
                                    CanRead    = per.CanRead,
                                    CanDelete  = per.CanDelete,
                                    CanUpdate  = per.CanUpdate,
                                };
                                _permissionService.Add(permission);
                            }
                        }
                    }
                    _permissionService.SaveChange();
                    response = request.CreateResponse(HttpStatusCode.OK, "Lưu quyền thành công");
                }
                return(response);
            };

            return(CreateHttpResponse(request, func));
        }