/// <summary> /// Initializes a new instance of the <see cref="UserCompetitionRights"/> class. /// </summary> /// <param name="competitionId"> /// The competition id. /// </param> /// <param name="user"> /// The user. /// </param> internal UserCompetitionRights(Guid competitionId, User user) { this.CompetitionId = competitionId; this.Permissions = new List<WinShooterCompetitionPermissions>(); this.rightsHelper.CurrentUser = user; this.Permissions.AddRange(this.rightsHelper.GetRightsForCompetitionIdAndTheUser(competitionId)); }
public void TestFetchingRightsFromDatabase() { var user = new User { Id = Guid.Parse("731bc7fd-1ab6-49ae-8056-92b507eef5f0") }; var competitionId = Guid.Parse("731bc7fd-1ab6-49ae-8056-92b507eef5e9"); var rights = new UserCompetitionRights(competitionId, user); rights.Permissions.Add(WinShooterCompetitionPermissions.CreateCompetition); Assert.IsTrue(rights.HasPermission(WinShooterCompetitionPermissions.CreateCompetition)); Assert.IsFalse(rights.HasPermission(WinShooterCompetitionPermissions.CreateUserCompetitionRole)); }
/// <summary> /// Make sure to have all tokens in database for the user. /// </summary> /// <param name="dbsession"> /// The database session. /// </param> /// <param name="user"> /// The user. /// </param> /// <param name="providerTokens"> /// The provider tokens. /// </param> private void AddAllTokensToUser(ISession dbsession, User user, IEnumerable<IOAuthTokens> providerTokens) { // Using all tokens, search for a user foreach (var authToken in providerTokens) { var userLoginInfo = (from info in dbsession.Query<UserLoginInfo>() where info.IdentityProvider == authToken.Provider && info.IdentityProviderId == authToken.UserId select info).SingleOrDefault(); if (userLoginInfo != null) { // A connection already exist continue; } using (var transaction = dbsession.BeginTransaction()) { var newUserLoginInfo = CreateLoginInfoFromAuthToken(authToken, user); dbsession.SaveOrUpdate(newUserLoginInfo); transaction.Commit(); this.UserLoginInfos.Add(newUserLoginInfo); } } }
/// <summary> /// Crates a new user. /// </summary> /// <param name="dbsession">Database session</param> /// <param name="providerOAuthAccess"> /// The provider information. /// </param> /// <returns> /// The <see cref="UserLoginInfo"/>. /// </returns> private static UserLoginInfo CreateNewUser(ISession dbsession, List<IOAuthTokens> providerOAuthAccess) { if (providerOAuthAccess.Count == 0) { throw new ArgumentOutOfRangeException("providerOAuthAccess"); } UserLoginInfo currentUserLoginInfo = null; string userDisplayName = null; string firstName = null; string lastName = null; using (var transaction = dbsession.BeginTransaction()) { var user = new User { LastLogin = DateTime.Now, LastUpdated = DateTime.Now }; dbsession.Save(user); foreach (var authToken in providerOAuthAccess) { var userLoginInfo = CreateLoginInfoFromAuthToken(authToken, user); currentUserLoginInfo = userLoginInfo; user.Email = authToken.Email; if (authToken.DisplayName != null && userDisplayName == null) { userDisplayName = authToken.DisplayName; } if (authToken.FirstName != null && firstName == null) { firstName = authToken.FirstName; } if (authToken.LastName != null && lastName == null) { lastName = authToken.LastName; } dbsession.Save(userLoginInfo); } if (firstName != null) { user.Givenname = firstName; } if (lastName != null) { user.Surname = lastName; } dbsession.Update(user); transaction.Commit(); } return currentUserLoginInfo; }
/// <summary> /// Create a <see cref="UserLoginInfo"/> from a provider token. /// </summary> /// <param name="authToken"> /// The token. /// </param> /// <param name="user"> /// The user. /// </param> /// <returns> /// The <see cref="UserLoginInfo"/>. /// </returns> private static UserLoginInfo CreateLoginInfoFromAuthToken(IOAuthTokens authToken, User user) { return new UserLoginInfo { User = user, LastLogin = DateTime.Now, IdentityProvider = authToken.Provider, IdentityProviderId = authToken.UserId, IdentityProviderUsername = authToken.Email }; }
/// <summary> /// Adds a new competition. /// </summary> /// <param name="user"> /// The user trying to add or update a competition. /// </param> /// <param name="competition"> /// The competition to add or update. /// </param> /// <returns> /// The <see cref="Guid"/> of the new competition. /// </returns> public Competition AddCompetition(User user, Competition competition) { competition.Id = Guid.NewGuid(); competition.VerifyDataContent(); var rolesLogic = new RolesLogic(); var role = rolesLogic.DefaultOwnerRole; if (role == null) { // This should never happen throw new NullReferenceException(string.Format("Default Owner Role \"{0}\" is not present.", rolesLogic.DefaultOwnerRoleName)); } var userRolesInfo = new UserRolesInfo { Competition = competition, Role = role, User = user }; using (var transaction = this.competitionRepository.StartTransaction()) { // First add the new competition to database this.competitionRepository.Add(competition); // Then add the user as admin this.userRolesInfoRepository.Add(userRolesInfo); // Commit transaction transaction.Commit(); } return competition; }