예제 #1
0
        public async Task <Mail> MCSendAsync(Mail mail)
        {
            var authId = GetClaimId(ClaimType.UserId);

            if (HasPermission(Permission.MailParticipants))
            {
                var user = await _userRepository.GetByIdAsync(mail.ToUserId.Value);

                if (user != null)
                {
                    mail.FromUserId           = 0;
                    mail.CanParticipantDelete = true;
                    mail.IsNew     = true;
                    mail.IsDeleted = false;
                    mail.SiteId    = GetClaimId(ClaimType.SiteId);

                    await _cache.RemoveAsync(UnreadMailCacheKey(mail.SiteId, (int)mail.ToUserId));

                    return(await _mailRepository.AddSaveAsync(authId, mail));
                }
                else
                {
                    throw new GraException("User doesn't exist");
                }
            }
            else
            {
                _logger.LogError($"User {authId} doesn't have permission to send a mail to {mail.ToUserId}.");
                throw new GraException("Permission denied");
            }
        }
예제 #2
0
        public async Task <Mail> SendAsync(Mail mail)
        {
            var siteId       = GetClaimId(ClaimType.SiteId);
            var authId       = GetClaimId(ClaimType.UserId);
            var activeUserId = GetActiveUserId();

            if (mail.ToUserId == null)
            {
                mail.FromUserId = activeUserId;
                mail.IsNew      = true;
                mail.IsDeleted  = false;
                mail.SiteId     = siteId;

                _cache.Remove(UnhandledMailCount(siteId));

                return(await _mailRepository.AddSaveAsync(authId, mail));
            }
            else
            {
                _logger.LogError($"User {activeUserId} doesn't have permission to send a mail to {mail.ToUserId}.");
                throw new GraException("Permission denied");
            }
        }
예제 #3
0
        public async Task <Drawing> PerformDrawingAsync(Drawing drawing)
        {
            // todo validate site
            int siteId     = GetCurrentSiteId();
            int authUserId = GetClaimId(ClaimType.UserId);

            if (HasPermission(Permission.PerformDrawing))
            {
                // insert drawing
                drawing.DrawingCriterion = default(DrawingCriterion);
                drawing.Id = default(int);
                drawing    = await _drawingRepository.AddSaveAsync(authUserId, drawing);

                // pull list of eligible users
                var eligibleUserIds
                    = await _drawingCriterionRepository.GetEligibleUserIdsAsync(drawing.DrawingCriterionId);

                // ensure there are enough eligible users to do the drawing
                if (drawing.WinnerCount > eligibleUserIds.Count())
                {
                    throw new GraException($"Cannot draw {drawing.WinnerCount} from an eligible pool of {eligibleUserIds.Count()} participants.");
                }

                // prepare and perform the drawing
                var remainingUsers = new List <int>(eligibleUserIds);
                var rng            = System.Security.Cryptography.RandomNumberGenerator.Create();
                var randomBytes    = new byte[sizeof(int)];

                for (int count = 1; count <= drawing.WinnerCount; count++)
                {
                    rng.GetBytes(randomBytes);
                    int random       = System.Math.Abs(System.BitConverter.ToInt32(randomBytes, 0));
                    int randomUserId = remainingUsers.ElementAt(random % remainingUsers.Count());

                    var winner = new PrizeWinner
                    {
                        SiteId    = siteId,
                        DrawingId = drawing.Id,
                        UserId    = randomUserId
                    };

                    // if there's an associated notification, send it now
                    if (!string.IsNullOrEmpty(drawing.NotificationSubject) &&
                        !string.IsNullOrEmpty(drawing.NotificationMessage))
                    {
                        var mail = new Mail
                        {
                            SiteId    = siteId,
                            Subject   = drawing.NotificationSubject,
                            Body      = drawing.NotificationMessage,
                            ToUserId  = winner.UserId,
                            DrawingId = drawing.Id
                        };
                        mail = await _mailRepository.AddSaveAsync(authUserId, mail);

                        winner.MailId = mail.Id;
                    }

                    // add the winner - does not perform a save
                    await _prizeWinnerRepository.AddAsync(authUserId, winner);

                    // remove them so they aren't drawn twice
                    remainingUsers.Remove(randomUserId);
                }
                await _drawingRepository.SaveAsync();

                // return the fully-populated drawing
                return(await _drawingRepository.GetByIdAsync(drawing.Id));
            }
            else
            {
                _logger.LogError($"User {authUserId} doesn't have permission to perform drawings.");
                throw new GraException("Permission denied.");
            }
        }
        public async Task InsertSampleData(int userId)
        {
            VerifyPermission(Permission.AccessFlightController);

            var user = await _userRepository.GetByIdAsync(userId);

            var programs = await _programRepository.GetAllAsync(user.SiteId);

            int prereaderProgramId = ProgramIdSearch(programs, "Prereader");
            int kidsProgramId      = ProgramIdSearch(programs, "Kid");
            int teensProgramId     = ProgramIdSearch(programs, "Teen");
            int adultProgramId     = ProgramIdSearch(programs, "Adult");

            //insert sample data
            var challenge = new Challenge
            {
                SiteId          = user.SiteId,
                RelatedSystemId = user.SystemId,
                BadgeId         = null,
                Name            = "Get Along",
                Description     = "This is a challenge encourging you to get along with others!",
                IsActive        = false,
                IsDeleted       = false,
                IsValid         = true,
                PointsAwarded   = 10,
                TasksToComplete = 2,
                RelatedBranchId = user.BranchId
            };

            challenge = await _challengeRepository.AddSaveAsync(userId, challenge);

            int positionCounter = 1;
            await _challengeTaskRepository.AddSaveAsync(userId, new ChallengeTask
            {
                ChallengeId       = challenge.Id,
                Title             = "Be excellent to each other",
                ChallengeTaskType = ChallengeTaskType.Action,
                Position          = positionCounter++
            });

            await _challengeTaskRepository.AddSaveAsync(userId, new ChallengeTask
            {
                ChallengeId       = challenge.Id,
                Title             = "Party on, dudes!",
                ChallengeTaskType = ChallengeTaskType.Action,
                Position          = positionCounter++
            });

            challenge = new Challenge
            {
                SiteId          = user.SiteId,
                BadgeId         = null,
                RelatedSystemId = user.SystemId,
                Name            = "Science Fiction reading list",
                Description     = "Read some excellent science fiction!",
                IsActive        = false,
                IsDeleted       = false,
                IsValid         = true,
                PointsAwarded   = 10,
                TasksToComplete = 2,
                RelatedBranchId = user.BranchId
            };

            challenge = await _challengeRepository.AddSaveAsync(userId, challenge);

            positionCounter = 0;

            await _challengeTaskRepository.AddSaveAsync(userId, new ChallengeTask
            {
                ChallengeId       = challenge.Id,
                Title             = "Slaughterhouse-Five, or The Children's Crusade: A Duty-Dance with Death",
                Author            = "Kurt Vonnegut",
                Isbn              = "978-0385333849",
                ChallengeTaskType = ChallengeTaskType.Book,
                Position          = positionCounter++
            });

            await _challengeTaskRepository.AddSaveAsync(userId, new ChallengeTask
            {
                ChallengeId       = challenge.Id,
                Title             = "Stories of Your Life and Others",
                Author            = "Ted Chiang",
                Isbn              = "978-1101972120",
                ChallengeTaskType = ChallengeTaskType.Book,
                Position          = positionCounter++
            });

            await _challengeTaskRepository.AddSaveAsync(userId, new ChallengeTask
            {
                ChallengeId       = challenge.Id,
                Title             = "Have Space Suit - Will Travel",
                Author            = "Robert A. Heinlein",
                Isbn              = "978-1416505495",
                ChallengeTaskType = ChallengeTaskType.Book,
                Position          = positionCounter++
            });

            var district = await _schoolService.AddDistrict(new SchoolDistrict()
            {
                Name = "International Confederation of Wizards"
            });

            var schoolHogwarts = await _schoolService.AddSchool("Hogwarts", district.Id);

            await _schoolService.AddSchool("Ilvermorny", district.Id);

            var schoolBeauxbatons = await _schoolService.AddSchool("Beauxbatons", district.Id);

            var schoolDurmstrang = await _schoolService.AddSchool("Durmstrang", district.Id);

            var userCheck = await _userRepository.GetByUsernameAsync("aweasley");

            if (userCheck == null)
            {
                // add some family users
                var newUser = new User
                {
                    SiteId    = user.SiteId,
                    BranchId  = user.BranchId,
                    SystemId  = user.SystemId,
                    ProgramId = adultProgramId,
                    FirstName = "Arthur",
                    LastName  = "Weasley",
                    Username  = "******"
                };

                var arthur = await _userRepository.AddSaveAsync(userId, newUser);

                await _userRepository.SetUserPasswordAsync(userId, arthur.Id, "123123");

                await _mailRepository.AddSaveAsync(userId, new Mail
                {
                    Body     = "Thanks for joining our reading program, Arthur. You're the best!",
                    ToUserId = arthur.Id,
                    IsNew    = true,
                    Subject  = "Welcome to the program!",
                    SiteId   = arthur.SiteId
                });

                await _activityService.LogActivityAsync(arthur.Id, 1);

                await _activityService.AddBookAsync(arthur.Id, new Book
                {
                    Author = "Kurt Vonnegut",
                    Title  = "Slaughterhouse-Five, or The Children's Crusade: A Duty-Dance with Death"
                });

                await _activityService.LogActivityAsync(arthur.Id, 1);

                await _activityService.AddBookAsync(arthur.Id, new Book
                {
                    Author = "Kurt Vonnegut",
                    Title  = "Breakfast of Champions, or Goodbye Blue Monday"
                });

                newUser.FirstName           = "Molly";
                newUser.Username            = null;
                newUser.HouseholdHeadUserId = arthur.Id;
                var molly = await _userRepository.AddSaveAsync(userId, newUser);

                await _activityService.LogActivityAsync(molly.Id, 1);

                await _activityService.AddBookAsync(molly.Id, new Book
                {
                    Author = "Kurt Vonnegut",
                    Title  = "Cat's Cradle"
                });

                newUser.FirstName = "Bill";
                await _userRepository.AddAsync(userId, newUser);

                newUser.FirstName = "Charlie";
                newUser.SchoolId  = schoolHogwarts.Id;
                newUser.ProgramId = teensProgramId;
                await _userRepository.AddAsync(userId, newUser);

                newUser.FirstName = "Fred";
                await _userRepository.AddAsync(userId, newUser);

                newUser.FirstName = "George";
                await _userRepository.AddAsync(userId, newUser);

                newUser.FirstName = "Percy";
                newUser.ProgramId = adultProgramId;
                newUser.SchoolId  = default(int?);
                await _userRepository.AddAsync(userId, newUser);

                await _userRepository.SaveAsync();

                newUser.FirstName           = "Ron";
                newUser.HouseholdHeadUserId = null;
                var ron = await _userRepository.AddSaveAsync(userId, newUser);

                newUser.FirstName           = "Hermione";
                newUser.LastName            = "Granger";
                newUser.HouseholdHeadUserId = ron.Id;
                var hermione = await _userRepository.AddSaveAsync(userId, newUser);

                await _activityService.LogActivityAsync(ron.Id, 1);

                await _activityService.LogActivityAsync(hermione.Id, 1);

                await _activityService.LogActivityAsync(hermione.Id, 1);

                await _activityService.LogActivityAsync(hermione.Id, 1);

                await _activityService.LogActivityAsync(hermione.Id, 1);

                await _activityService.LogActivityAsync(hermione.Id, 1);

                newUser.ProgramId = kidsProgramId;
                newUser.FirstName = "Rose";
                newUser.LastName  = "Granger-Weasley";
                newUser.SchoolId  = schoolBeauxbatons.Id;
                await _userRepository.AddAsync(userId, newUser);

                newUser.ProgramId = prereaderProgramId;
                newUser.FirstName = "Hugo";
                await _userRepository.AddAsync(userId, newUser);

                await _userRepository.SaveAsync();

                newUser.FirstName           = "Harry";
                newUser.LastName            = "Potter";
                newUser.ProgramId           = adultProgramId;
                newUser.HouseholdHeadUserId = null;
                newUser.SchoolId            = default(int?);
                var harry = await _userRepository.AddSaveAsync(userId, newUser);

                newUser.FirstName           = "Ginevra";
                newUser.HouseholdHeadUserId = harry.Id;
                var ginny = await _userRepository.AddSaveAsync(userId, newUser);

                await _activityService.LogActivityAsync(harry.Id, 1);

                await _activityService.LogActivityAsync(harry.Id, 1);

                await _activityService.LogActivityAsync(ginny.Id, 1);

                newUser.FirstName = "James";
                newUser.ProgramId = teensProgramId;
                newUser.SchoolId  = schoolDurmstrang.Id;
                await _userRepository.AddAsync(userId, newUser);

                newUser.FirstName = "Albus";
                newUser.ProgramId = kidsProgramId;
                newUser.SchoolId  = schoolHogwarts.Id;
                await _userRepository.AddAsync(userId, newUser);

                newUser.FirstName = "Lily";
                newUser.SchoolId  = default(int?);
                await _userRepository.AddAsync(userId, newUser);

                await _userRepository.SaveAsync();
            }
        }