Example #1
0
        public async Task <IActionResult> Edit(int id, [FromForm] PlatformViewModel platformViewModel, [FromServices] UserManager <User> userManager)
        {
            if (ModelState.IsValid)
            {
                var platform = _platformManager.GetPlatform(id);

                platform.Name        = platformViewModel.Name;
                platform.Tenant      = platformViewModel.Tenant;
                platform.Description = platformViewModel.Description;

                platform.ColorScheme.SocialBarColor  = platformViewModel.SocialBarColor;
                platform.ColorScheme.BannerColor     = platformViewModel.BannerColor;
                platform.ColorScheme.ButtonColor     = platformViewModel.ButtonColor;
                platform.ColorScheme.ButtonTextColor = platformViewModel.ButtonTextColor;
                platform.ColorScheme.TextColor       = platformViewModel.TextColor;
                platform.ColorScheme.BodyColor       = platformViewModel.BodyColor;

                if (platformViewModel.LogoChanged)
                {
                    var fileName = Util.Util.GenerateDataStoreObjectName(platformViewModel.Name);
                    var imageObj = new Media()
                    {
                        Name = fileName,
                        Url  = await _fileUploader.UploadFile(fileName, "platform-logos", platformViewModel.Logo),
                    };
                    platform.Logo = imageObj;
                }

                if (platformViewModel.BannerChanged)
                {
                    var fileName = Util.Util.GenerateDataStoreObjectName(platformViewModel.Name);
                    var imageObj = new Media()
                    {
                        Name = fileName,
                        Url  = await _fileUploader.UploadFile(fileName, "platform-banners", platformViewModel.Banner),
                    };
                    platform.Banner = imageObj;
                }

                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"));
                }

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

                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.UpdatePlatform(platform);
                _unitOfWorkManager.Save();
                return(Ok());
            }

            return(StatusCode(400));
        }