Ejemplo n.º 1
0
		/// <summary>
		/// Registers a user.
		/// </summary>
		public async Task<RegisterNewUserResult> RegisterNewStudentAsync(
			string classroomName,
			string sectionName,
			StudentRegistration registration,
			string confirmationUrlBuilder,
			IModelErrorCollection errors)
		{
			var section = _dbContext.Sections
				.Where(s => s.Classroom.Name == classroomName)
				.Include(s => s.Classroom)
				.SingleOrDefault(s => s.Name == sectionName);

			if (section == null)
			{
				return RegisterNewUserResult.SectionNotFound;
			}

			if (!section.AllowNewRegistrations)
			{
				return RegisterNewUserResult.SectionNotOpen;
			}

			var user = await GetAndUpdateCurrentUserAsync();
			if (user != null)
			{
				return RegisterNewUserResult.AlreadyRegistered;
			}
			
			if (!await _gitHubUserClient.DoesUserExistAsync(registration.GitHubLogin))
			{
				errors.AddError("GitHubLogin", "The GitHub username does not exist.");
				return RegisterNewUserResult.Failed;
			}

			user = new User()
			{
				UniqueId = _identityProvider.CurrentIdentity.UniqueId,
				UserName = _identityProvider.CurrentIdentity.UserName,
				FirstName = registration.FirstName,
				LastName = _identityProvider.CurrentIdentity.LastName,
				EmailAddress = registration.EmailAddress,
				EmailConfirmationCode = GenerateEmailConfirmationCode(),
				EmailAddressConfirmed = false,
				GitHubLogin = registration.GitHubLogin,
				SuperUser = false
			};

			var membership = await EnsureSectionMembershipAsync(user, section, SectionRole.Student);		
			await EnsureUserInGithubOrgAsync(user, membership.ClassroomMembership);
			await SendUserInvitationMailAsync(user, confirmationUrlBuilder);

			_dbContext.Users.Add(user);
			await _dbContext.SaveChangesAsync();

			return RegisterNewUserResult.Success;
		}
Ejemplo n.º 2
0
		public async Task<IActionResult> AddNewStudent(
			string classroomName, 
			string sectionName, 
			StudentRegistration registration)
		{
			var section = await GetSectionAsync(classroomName, sectionName);
			if (section == null)
			{
				return NotFound();
			}

			if (!section.AllowNewRegistrations)
			{
				return RedirectToAction("Register");
			}

			var emailConfirmationUrl = GetEmailConfirmationUrlBuilder();

			var result = await _userService.RegisterNewStudentAsync
			(
				classroomName,
				sectionName,
				registration, 
				emailConfirmationUrl, 
				ModelErrors
			);

			switch (result)
			{
				case RegisterNewUserResult.SectionNotFound:
					return NotFound();

				case RegisterNewUserResult.SectionNotOpen:
				case RegisterNewUserResult.AlreadyRegistered:
					return RedirectToAction("Register");

				case RegisterNewUserResult.Failed:
					PopulateRegistrationViewBag(section);
					return View("RegisterNewStudent", registration);

				case RegisterNewUserResult.Success:
					var currentUser = await _userService.GetAndUpdateCurrentUserAsync();
					return RedirectToAction("View", new { userId = currentUser.Id });

				default:
					throw new InvalidOperationException("Invalid result from RegisterNewUserAsync.");
			}
		}