예제 #1
0
        private async Task AddUserAsDelegateForOrganization(Organization organization, User user)
        {
            OrganizationUser orgUser = new OrganizationUser
            {
                OrganizationId = organization.Id,
                Organization = organization,
                UserId = user.Id,
                User = user,
                Role = OrganizationRole.Delegate
            };
            orgUser.UpdateTrackingData(user);

            unitOfWork.GetRepository<OrganizationUser>().Insert(orgUser);
            await unitOfWork.SaveAsync();
        }
        private async Task AssociateUser(Guid organizationId, string userId, User user)
        {
            var assocUser = await userManager.FindByIdAsync(userId);

            if (!assocUser.OrganizationAssociations.Any(oa => oa.OrganizationId == organizationId))
            {
                OrganizationUser orgUser = new OrganizationUser
                {
                    OrganizationId = organizationId,
                    UserId = userId,
                    Role = OrganizationRole.Member
                };
                orgUser.UpdateTrackingData(user);
                assocUser.OrganizationAssociations.Add(orgUser);
                await userManager.UpdateAsync(assocUser);
            }
        }
예제 #3
0
        public async Task<ActionResult> Register(
            User user,
            Guid? teamId = null,
            Guid? organizationId = null,
            Guid? tournamentId = null,
            String replacesPerson = null,
            String returnUrl = "")
        {

            #region Check teamId and permissions
            // Check teamId, organizationId, tournamentId and user permissions

            Team team = null;
            User authenticatedUser = null;

            if (teamId != null || organizationId != null)
            {

                // Get user

                var ident = HttpContext.User.Identity as ClaimsIdentity;

                if (ident != null)
                {
                    var userId = ident.GetUserId();
                    authenticatedUser = await userManager.FindByIdAsync(userId);

                    if (authenticatedUser != null)
                    {

                        // Get organizationId from team, if necessary

                        if (organizationId == null)
                        {
                            team = tournamentRegistrationsManager.GetTeam((Guid)teamId);

                            if (team != null)
                            {
                                organizationId = team.OrganizationId;
                                tournamentId = team.TournamentId;
                            }


                        }

                        // Check user permissions

                        if (!userManager.HasOrganizationRole(userId, (Guid)organizationId, OrganizationRole.Delegate))
                        {
                            return RedirectToAction("Index", "Home");
                        }

                        // Associate user with organization

                        var organization = organizationManager.GetOrganization((Guid)organizationId);

                        OrganizationUser orgUser = new OrganizationUser
                        {
                            Organization = organization,
                            User = user,
                            Role = OrganizationRole.Member,
                        };

                        user.OrganizationAssociations.Add(orgUser);
                        user.CurrentOrganization = organization;
                    }
                }

                // authenticatedUser is only null, if something was wrong

                if (authenticatedUser == null)
                {
                    return RedirectToAction("Index", "Home");

                }
            }

            #endregion

            if (ModelState.IsValid)
            {
                // store e-mail lower case
                user.Email = user.Email.ToLower();

                // set Username to e-mail address
                user.UserName = user.Email;

                // Create user
                IdentityResult result = await userManager.CreateAsync(user);

                // Process result
                if (result.Succeeded)
                {
                    user = await userManager.FindByNameAsync(user.Email);

                    // Generate reset password token

                    var token = userManager.GeneratePasswordResetToken(user.Id);
                    var resetUrl = this.Url.Action(
                        "ResetPassword",
                        null,
                        new
                        {
                            userId = user.Id,
                            token = token,
                            returnUrl = returnUrl
                        },
                        Request.Url.Scheme
                    );

                    var sponsoringOrganization = organizationManager.GetOrganization(user.SponsoringOrganizationId);

                    sendMail.SponsoringOrganization = sponsoringOrganization;
                    sendMail.UserRegistered(user, resetUrl);

                    // user registration without team, tournamentId, organizationId

                    if (teamId == null && (tournamentId == null || organizationId == null))
                    {
                        // await Signin(user); //signin, so that Registered method has the user. Will be signed out in Registered.
                        return View("Registered", new UserViewModel
                        {
                            User = user
                        });
                    }

                    // user registration with team

                    if (teamId != null)
                    {
                        user = userManager.FindByName(user.Email);

                        if (replacesPerson != null)
                        {
                            tournamentRegistrationsManager.RemoveSpeaker((Guid)teamId, replacesPerson, authenticatedUser);

                        }

                        await tournamentRegistrationsManager.AddSpeakerAsync((Guid)teamId, user.Id, authenticatedUser);
                    }

                    // user registration as adjudidicator

                    else if (organizationId != null & tournamentId != null)
                    {
                        if (replacesPerson != null)
                        {
                            tournamentRegistrationsManager.RemoveAdjudicator((Guid)tournamentId, replacesPerson, authenticatedUser);
                        }

                        await tournamentRegistrationsManager.AddAdjudicatorAsync((Guid)tournamentId, (Guid)organizationId, user.Id, authenticatedUser);
                    }

                    // if we have a returnUrl, redirect to it

                    if (!String.IsNullOrWhiteSpace(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }

                    return View("Registered", new UserViewModel
                    {
                        User = user
                    });

                }
                else
                {
                    AddErrorsFromResult(result);
                }
            }
            return View(user);
        }
        public async Task<SetTeamOrAdjudicatorResult> AddSpeakerAsync(Guid teamId, String speakerId, User user)
        {
            var team = GetTeam(teamId);

            // Get and check team
            if (team == null)
            {
                return SetTeamOrAdjudicatorResult.TeamNotFound;
            }

            // Check maximum number of speakers

            if (team.Speaker.Count >= team.Tournament.TeamSize)
            {
                return SetTeamOrAdjudicatorResult.TooManySpeakers;
            }

            // check if speaker is already in team

            if (team.Speaker.Any(sp => sp.Id == speakerId))
            {
                return SetTeamOrAdjudicatorResult.SpeakerAlreadyInTeam;
            }

            // Get and check user

            var speaker = await userManager.FindByIdAsync(speakerId);

            if (speaker == null)
            {
                return SetTeamOrAdjudicatorResult.UserNotFound;
            }

            // check if speaker is already in other team

            var teams = GetTeams(team.TournamentId);
            var tournamentSpeakers = GetSpeakers(team.TournamentId, teamId);
            if (tournamentSpeakers.Any(tsp => tsp.Id == speakerId))
            {
                return SetTeamOrAdjudicatorResult.SpeakerAlreadyInOtherTeam;
            }

            // check if speaker is already adjudicator

            var adjudicators = GetAdjudicators(team.TournamentId);
            if (adjudicators.Any(adj => adj.UserId == speakerId))
            {
                return SetTeamOrAdjudicatorResult.SpeakerAlreadyAdjudicator;
            }

            // add user to organization if needed

            if (!speaker.OrganizationAssociations.Any(oa => oa.OrganizationId == team.OrganizationId))
            {
                OrganizationUser orgUser = new OrganizationUser
                {
                    OrganizationId = team.Organization.Id,
                    Organization = team.Organization,
                    User = speaker,
                    UserId = speaker.Id,
                    Role = OrganizationRole.Member
                };

                unitOfWork.GetRepository<OrganizationUser>().Insert(orgUser);
                await unitOfWork.SaveAsync();
            }

            // add user to team

            team.Speaker.Add(speaker);
            team.UpdateTrackingData(user);
            unitOfWork.GetRepository<Team>().Update(team);
            await unitOfWork.SaveAsync();
            return SetTeamOrAdjudicatorResult.TeamUpdated;
        }
예제 #5
0
        public void Init()
        {
            // Create user for finance and tournament management

            DebRegDataMocks dataMocks = new DebRegDataMocks();
            DebRegUserManager userManager = dataMocks.UserManager;
            {
                var task = userManager.CreateAsync(financeManagerUser);
                if (!task.IsCompleted)
                {
                    task.Wait();
                }
            }

            // Create hosting organization

            SecurityMocks securityMocks = new SecurityMocks();
            IAuthenticationManager authManager = securityMocks.AuthManager;
            ISecurityManager securityManager = new SecurityManager(userManager, authManager);

            IUnitOfWork unitOfWork = dataMocks.UnitOfWork;
            IOrganizationManager organizationManager = new OrganizationManager(unitOfWork, userManager);
            {
                var task = organizationManager.CreateOrganizationAsync(hostingOrganization, financeManagerUser);
                if (!task.IsCompleted)
                {
                    task.Wait();
                }
            }

            // Make user tournament manager

            OrganizationUser organizationUser = new OrganizationUser
            {
                Organization = hostingOrganization,
                OrganizationId = hostingOrganization.Id,
                User = financeManagerUser,
                UserId = financeManagerUser.Id,
                Role = OrganizationRole.OrganizationTournamentManager
            };
            financeManagerUser.OrganizationAssociations.Add(organizationUser);
            hostingOrganization.UserAssociations.Add(organizationUser);
            financeManagerUser.OrganizationAssociations.Add(organizationUser);
            unitOfWork.GetRepository<OrganizationUser>().Insert(organizationUser);
            unitOfWork.Save();

            // Create tournament

            ITournamentManager tournamentManager = new TournamentManager(unitOfWork, userManager);

            tournament = new Tournament
            {
                Id = Guid.NewGuid(),
                HostingOrganization = hostingOrganization,
                HostingOrganizationID = hostingOrganization.Id
            };
            {
                var task = tournamentManager.AddTournamentAsync(tournament, financeManagerUser);
                if (!task.IsCompleted)
                {
                    task.Wait();
                }
            }

            // Create Controller

            DebRegCommunicationMocks communicationMocks = new DebRegCommunicationMocks();
            IEMailService mailService = communicationMocks.EMailService;

            registrationManager = new TournamentRegistrationsManager(unitOfWork, mailService, userManager);
            IBookingManager bookingManager = new BookingManager(unitOfWork);
            IPaymentManager paymentManager = new PaymentManager(registrationManager, bookingManager, mailService);

            financeController = new FinanceController(tournamentManager, registrationManager, paymentManager, bookingManager, userManager);

            // Set controller context
            httpMocks.UserId = financeManagerUser.Id;
            httpMocks.UserName = financeManagerUser.UserName;

            financeController.ControllerContext = httpMocks.ControllerContext;
        }