コード例 #1
0
        public async Task <IActionResult> BecomeTrainer(ClientToTrainerBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(model));
            }

            var result = await this.usersService.BecomeTrainerAsync(model, this.User.Identity.Name);

            if (!result)
            {
                return(this.Redirect($"AreaClient/Client/BecomeTrainer?username={model.Username}"));
            }

            await this.signInManager.SignOutAsync();

            return(this.Redirect(GlobalConstants.Index));
        }
コード例 #2
0
ファイル: UsersService.cs プロジェクト: NickolayNanov/Olympia
        public async Task <bool> BecomeTrainerAsync(ClientToTrainerBindingModel model, string username)
        {
            OlympiaUser realUser = this.context.Users.SingleOrDefault(user => user.UserName == username);

            realUser.Age         = model.Age;
            realUser.Description = model.Description;
            realUser.Email       = model.Email;
            realUser.FullName    = model.FullName;
            realUser.Weight      = model.Weight;
            realUser.Height      = model.Height;

            if (model.ProfilePictureUrl != null)
            {
                var url = MyCloudinary.UploadImage(model.ProfilePictureUrl, model.Username);
                realUser.ProfilePicturImgUrl = url ?? Constants.CloudinaryInvalidUrl;
            }

            this.context.Update(realUser);
            await this.context.SaveChangesAsync();

            await this.userManager.UpdateSecurityStampAsync(realUser);

            var userRemovedFromRole = await this.userManager.RemoveFromRoleAsync(realUser, GlobalConstants.ClientRoleName);

            var roleHasChanged = await this.userManager.AddToRoleAsync(realUser, GlobalConstants.TrainerRoleName);

            if (!roleHasChanged.Succeeded)
            {
                return(false);
            }

            await this.userManager.UpdateAsync(realUser);

            this.context.Update(realUser);
            await this.context.SaveChangesAsync();

            var result = await this.userManager.IsInRoleAsync(realUser, GlobalConstants.TrainerRoleName);

            return(result);
        }