Beispiel #1
0
        /// <summary>
        /// Role editing method
        /// </summary>
        /// <param name = "id"> Id </param>
        /// <param name = "data"> Data </param>
        public void Edit(string id, RoleSubmitM data)
        {
            var role = _context.Roles.Find(id);

            if (role == null)
            {
                throw new Exception(nameof(Resource.RoleNotFound));
            }

            if (role.Name != data.Name &&
                _context.Roles.Any(r => r.Name == data.Name))
            {
                throw new Exception(nameof(Resource.NameError));
            }

            role.Name = data.Name;
            role.AccessToSystemFunctionsArray = data.AccessToSystemFunctions;
            role.StandardDataAccessArray      = data.StandardDataAccess.ToPermissionsModel();
            role.Description   = data.Description;
            role.SqlWalletUser = data.SqlWalletUser;
            _context.SaveChanges();
        }
Beispiel #2
0
        /// <summary>
        /// Role creation method
        /// </summary>
        /// <param name = "data"> Data </param>
        /// <returns> </returns>
        public RoleVm Create(RoleSubmitM data)
        {
            if (_context.Roles.Any(r => r.Name == data.Name))
            {
                throw new Exception(nameof(Resource.NameError));
            }

            var role = new Role
            {
                Name        = data.Name,
                Description = data.Description,
                AccessToSystemFunctionsArray = data.AccessToSystemFunctions,
                StandardDataAccessArray      = data.StandardDataAccess.ToPermissionsModel(),
                NormalizedName = data.Name.ToUpper(),
                Status         = RoleStatuses.Active,
                SqlWalletUser  = data.SqlWalletUser
            };

            _context.Roles.Add(role);
            _context.SaveChanges();

            return(RoleVm.Create(role));
        }
Beispiel #3
0
 public IActionResult Edit(string id, [FromBody] RoleSubmitM data)
 {
     _roleService.Edit(id, data);
     return(NoContent());
 }
Beispiel #4
0
        public IActionResult Create([FromBody] RoleSubmitM data)
        {
            var createdRoleVm = _roleService.Create(data);

            return(Created($"api/roles/{createdRoleVm.Id}", createdRoleVm));
        }
Beispiel #5
0
        public void CreateTest()
        {
            using (var context = CreateDbContext())
            {
                var submitData =
                    new RoleSubmitM
                {
                    Name               = "Role",
                    Description        = "Description",
                    StandardDataAccess = new DataAccessModel()
                    {
                        LocalUnit = new List <DataAccessAttributeVm>()
                        {
                            new DataAccessAttributeVm {
                                Name = DataAccessAttributesHelper.GetName <LegalUnit>("ForeignCapitalShare"), Allowed = true, CanRead = true, CanWrite = true
                            }
                        },
                        LegalUnit = new List <DataAccessAttributeVm>()
                        {
                            new DataAccessAttributeVm {
                                Name = DataAccessAttributesHelper.GetName <LocalUnit>("LegalUnitIdDate"), Allowed = true, CanRead = true, CanWrite = true
                            }
                        },
                        EnterpriseGroup = new List <DataAccessAttributeVm>()
                        {
                            new DataAccessAttributeVm {
                                Name = DataAccessAttributesHelper.GetName <EnterpriseGroup>("LiqReason"), Allowed = true, CanRead = true, CanWrite = true
                            }
                        },
                        EnterpriseUnit = new List <DataAccessAttributeVm>()
                        {
                            new DataAccessAttributeVm {
                                Name = DataAccessAttributesHelper.GetName <EnterpriseUnit>("Employees"), Allowed = true, CanRead = true, CanWrite = true
                            }
                        },
                    },
                    AccessToSystemFunctions = new List <int> {
                        1, 2, 3
                    },
                    ActiviyCategoryIds = new int[] {}
                };

                var  role     = new RoleService(context).Create(submitData);
                var  expected = typeof(Exception);
                Type actual   = null;
                try
                {
                    new RoleService(context).Create(submitData);
                }
                catch (Exception e)
                {
                    actual = e.GetType();
                }

                Assert.Equal(role.Name,
                             context.Roles.Single(
                                 x =>
                                 x.Name == submitData.Name && x.Status == RoleStatuses.Active &&
                                 x.Description == submitData.Description &&
                                 x.StandardDataAccess == JsonConvert.SerializeObject(submitData.StandardDataAccess.ToPermissionsModel()) &&
                                 x.AccessToSystemFunctions == "1,2,3"
                                 ).Name);
                Assert.Equal(expected, actual);
            }
        }
Beispiel #6
0
        public void EditTest()
        {
            using (var context = CreateDbContext())
            {
                var role = new Role
                {
                    AccessToSystemFunctionsArray = new List <int> {
                        1, 3
                    },
                    Name = "Role Name",
                    StandardDataAccessArray = new DataAccessPermissions(
                        new List <string> {
                        "LocalUnit.1", "LegalUnit.2", "EnterpriseUnit.3", "EnterpriseGroup.4"
                    }
                        .Select(x => new Permission(x, true, true)))
                    ,
                    Status = RoleStatuses.Active
                };
                context.Roles.Add(role);
                context.SaveChanges();
                context.Entry(role).State = EntityState.Detached;

                var daa = new DataAccessModel()
                {
                    LocalUnit = new List <DataAccessAttributeVm>()
                    {
                        new DataAccessAttributeVm {
                            Name = DataAccessAttributesHelper.GetName <LegalUnit>("ForeignCapitalShare"), Allowed = true, CanRead = true, CanWrite = true
                        }
                    },
                    LegalUnit = new List <DataAccessAttributeVm>()
                    {
                        new DataAccessAttributeVm {
                            Name = DataAccessAttributesHelper.GetName <LocalUnit>("LegalUnitIdDate"), Allowed = true, CanRead = true, CanWrite = true
                        }
                    },
                    EnterpriseGroup = new List <DataAccessAttributeVm>()
                    {
                        new DataAccessAttributeVm {
                            Name = DataAccessAttributesHelper.GetName <EnterpriseGroup>("LiqReason"), Allowed = true, CanRead = true, CanWrite = true
                        }
                    },
                    EnterpriseUnit = new List <DataAccessAttributeVm>()
                    {
                        new DataAccessAttributeVm {
                            Name = DataAccessAttributesHelper.GetName <EnterpriseUnit>("Employees"), Allowed = true, CanRead = true, CanWrite = true
                        }
                    },
                };

                var roleData = new RoleSubmitM
                {
                    Name = "Edited Role Name",
                    AccessToSystemFunctions = new List <int> {
                        1, 2, 3
                    },
                    StandardDataAccess = daa,
                    Description        = "After Edit",
                    ActiviyCategoryIds = new int[] { }
                };

                new RoleService(context).Edit(role.Id, roleData);
                var single = context.Roles.Single(x => x.Id == role.Id);

                Assert.Equal(roleData.Name, single.Name);
                Assert.Equal(role.Status, single.Status);
                Assert.Equal(roleData.Description, single.Description);
                Assert.Equal(roleData.AccessToSystemFunctions, single.AccessToSystemFunctionsArray);
                Assert.Equal(JsonConvert.SerializeObject(roleData.StandardDataAccess.ToPermissionsModel()), single.StandardDataAccess);
            }
        }