Exemplo n.º 1
0
        public async Task <IActionResult> Create([FromForm] PlatformViewModel platformViewModel, [FromServices] UserManager <User> userManager)
        {
            if (ModelState.IsValid)
            {
                var stylesheet = new ColorScheme()
                {
                    SocialBarColor  = platformViewModel.SocialBarColor,
                    NavBarColor     = platformViewModel.NavbarColor,
                    BannerColor     = platformViewModel.BannerColor,
                    ButtonColor     = platformViewModel.ButtonColor,
                    ButtonTextColor = platformViewModel.ButtonTextColor,
                    TextColor       = platformViewModel.TextColor,
                    BodyColor       = platformViewModel.BodyColor,
                };

                Media logoImageObj = null;
                if (platformViewModel.Logo != null)
                {
                    var logoFileName = Util.Util.GenerateDataStoreObjectName(platformViewModel.Logo.FileName);
                    logoImageObj = new Media()
                    {
                        Name = logoFileName,
                        Url  = await _fileUploader.UploadFile(logoFileName, "platform-logos", platformViewModel.Logo),
                    };
                }



                Media bannerImageObj = null;
                if (platformViewModel.Banner != null)
                {
                    var bannerFileName = Util.Util.GenerateDataStoreObjectName(platformViewModel.Banner.FileName);
                    bannerImageObj = new Media()
                    {
                        Name = bannerFileName,
                        Url  = await _fileUploader.UploadFile(bannerFileName, "platform-banners", platformViewModel.Banner),
                    };
                }


                var platform = new Platform()
                {
                    Name        = platformViewModel.Name,
                    Tenant      = platformViewModel.Tenant,
                    Logo        = logoImageObj,
                    Banner      = bannerImageObj,
                    Description = platformViewModel.Description,
                    ColorScheme = stylesheet,
                };

                List <User> admins = userManager.GetUsersForClaimAsync(new Claim(platform.Tenant, "Admin")).Result.ToList();

                foreach (User admin in admins)
                {
                    await userManager.ReplaceClaimAsync(admin, new Claim(platform.Tenant, "Admin"),
                                                        new Claim(platform.Tenant, "User"));
                }


                if (platformViewModel.Admins != null)
                {
                    foreach (string AdminUserName in platformViewModel.Admins)
                    {
                        User newAdmin = userManager.FindByNameAsync(AdminUserName).Result;
                        //Kijken of user al bestaat op het platform
                        var claimsForSubdomain = userManager.GetClaimsAsync(newAdmin).Result.FirstOrDefault(c => c.Type == platform.Tenant); //Subdomain is het subdomain waarop je zit
                        if (claimsForSubdomain == null)
                        {
                            //User heeft nog geen claim op dit platform => claim toewijzen dus! EN DIRECT ADMIN TOEWIJZEN
                            await userManager.AddClaimAsync(newAdmin, new Claim(platform.Tenant, "Admin"));
                        }
                        else
                        {
                            //User heeft al een claim op dit platform -> Claim verwijderen is Admin Vervangen door User

                            /*await userManager.ReplaceClaimAsync(newAdmin, new Claim(claimsForSubdomain.Type, claimsForSubdomain.Value),
                             *  new Claim(claimsForSubdomain.Type, "User"));*/

                            //User heeft al een claim op dit platform en wordt nu admin -> Claim verwijderen is User Vervangen door Admin
                            await userManager.ReplaceClaimAsync(newAdmin, new Claim(claimsForSubdomain.Type, claimsForSubdomain.Value),
                                                                new Claim(claimsForSubdomain.Type, "Admin"));
                        }
                    }
                }


                _platformManager.AddPlatform(platform);
                _unitOfWorkManager.Save();
                return(Ok());
            }



            return(StatusCode(400));
        }