Exemple #1
0
        public async void CreateRolesAsync(IContainer container)
        {
            IdentityRoleManager roleManager = container.Resolve <IdentityRoleManager>();

            IdentityUserManager userManager = container.Resolve <IdentityUserManager>();

            if (userManager.FindById(6) != null)
            {
                await userManager.AddToRoleAsync(6, Role.Admin.ToString());
            }

            if (userManager.FindById(7) != null)
            {
                await userManager.AddToRoleAsync(7, Role.Seller.ToString());
            }

            if (roleManager.FindByNameAsync(Role.Admin.ToString()).Result == null)
            {
                await roleManager.CreateAsync(new CustomRole(Role.Admin.ToString()));

                await userManager.AddToRoleAsync(1, Role.Admin.ToString());
            }
            if (roleManager.FindByNameAsync(Role.Seller.ToString()).Result == null)
            {
                await roleManager.CreateAsync(new CustomRole(Role.Seller.ToString()));
            }
            if (roleManager.FindByNameAsync(Role.Customer.ToString()).Result == null)
            {
                await roleManager.CreateAsync(new CustomRole(Role.Customer.ToString()));
            }
        }
        public override async Task <IActionResult> Create([FromBody] Role role)
        {
            if (ModelState.IsValid)
            {
                var found = await Service.AnyAsync(e => e.CompanyId == role.CompanyId && e.Name == role.Name);

                if (found)
                {
                    ModelState.AddModelError("Errors", "Role already exists.");
                    return(StatusCode(StatusCodes.Status409Conflict, found));
                }
                role.TenantId = tenant.Id;
                IdentityResult result = await roleManager.CreateAsync(role);

                if (result.Succeeded)
                {
                    return(CreatedAtAction("Get", new { id = role.Id }, role));
                }
            }
            return(BadRequest());
        }