コード例 #1
0
        private AppOwner GetCurrentGroup()
        {
            AppOwner group = this._appOwnerRepository.FindById(this.OwnerId);

            Debug.Assert(group != null, "Something went terribly wrong...");
            return(group);
        }
コード例 #2
0
 public void Delete(AppOwner item)
 {
     if (item != null)
     {
         this._entitySet.Remove(item);
     }
 }
コード例 #3
0
        /// <summary>
        /// Assigns an owner to the specified entity
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="ownerId"></param>
        public void AssignOwner(IAppOwnerEntity entity, int ownerId)
        {
            AppOwner owner = this._appOwnerRepository.FindById(ownerId);

            Debug.Assert(owner != null);

            entity.Owner = owner;
        }
コード例 #4
0
 public static AppUser Create(string name, string email, AppOwner group)
 {
     return(new AppUser {
         UserName = name,
         Email = email,
         Group = group
     });
 }
コード例 #5
0
        public void DeleteById(int id)
        {
            AppOwner item = this.FindById(id);

            if (item != null)
            {
                this._entitySet.Remove(item);
            }
        }
コード例 #6
0
        public async Task <IActionResult> ChangeActiveGroup([FromBody] ChangeGroupModel changeGroupModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            // Determine the group to change to and save
            AppUser user = await this._appUserManager.FindByIdAsync(this.User.Identity.GetUserId()).EnsureNotNull(HttpStatusCode.Forbidden);

            AppOwner desiredGroup = (user.AvailableGroups.FirstOrDefault(x => x.GroupId == changeGroupModel.GroupId)?.Group ?? user.AvailableImpersonations.GetForGroup(changeGroupModel.GroupId)?.Group).EnsureNotNull(HttpStatusCode.Forbidden);

            await this._appOwnerTokenChangeService.ChangeActiveGroupAsync(this.User, user, desiredGroup, this.HttpContext);

            return(await this.ReturnAuthenticationInfoResult(user));
        }
コード例 #7
0
        private static void SeedAccounts(AppDbContext context)
        {
            DbSet <AppOwner> appOwners = context.Set <AppOwner>();

            // add initial user
            var owner = new AppOwner("Initial");

            appOwners.Add(owner);

            AppUser user = AppUser.Create("User", "*****@*****.**", owner);
            var     svc  = new AppUserManager(new AppUserStore(context));

            svc.Create(user, "welcome01");

            context.SaveChanges();
        }
コード例 #8
0
        public async Task <AuthenticationInfo> Impersonate(int id)
        {
            AppUser currentUser = await this.GetCurrentUser();

            try {
                AppOwner impersonationGroup =
                    await this._appImpersonationTokenService.GetImpersonationUserGroup(currentUser, id);

                await this._appOwnerTokenChangeService.ChangeActiveGroupAsync(this.User, currentUser, impersonationGroup, this.HttpContext);

                return(await this._authenticationInfoFactory.CreateAsync(currentUser, this.User));
            }
            catch (ImpersonationNotAllowedException) {
                throw new HttpStatusException(HttpStatusCode.Forbidden);
            }
        }
コード例 #9
0
        private async Task SendDigestInternal(AppOwner appOwner, List <AppUser> users)
        {
            MonthlyDigestData data = await this._monthlyDigestDataFactory.GetForAsync(appOwner.Id);

            data.AppOwnerName = appOwner.Name;

            foreach (AppUser appUser in users)
            {
                if (!appUser.EmailConfirmed)
                {
                    this._logger.LogWarning($"Job for app owner #{appOwner.Id}: skip digest for {appUser.UserName} #{appUser.Id} because the e-mail address is not valid or confirmed [{appUser.Email}]");
                    continue;
                }

                await this._monthlyDigestMailer.SendAsync(appUser.Email, data);
            }
        }
コード例 #10
0
        public async Task Execute(int appOwnerId)
        {
            // Set a culture, we currently only support hardcoded NL
            CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("nl-NL");

            // Retrieve the user
            AppOwner appOwner = await this._appOwnerRepository.FindByIdAsync(appOwnerId);

            if (appOwner == null)
            {
                this._logger.LogError($"Unable to process job for app owner #{appOwnerId}: entity cannot be found");
                return;
            }

            // We only want to invoke if we have not sent a digest about last month
            DateTime invocationThreshold = DateTime.Now.AddMonths(-1);

            invocationThreshold = new DateTime(invocationThreshold.Year, invocationThreshold.Month, 1);

            if (appOwner.LastMonthlyDigestTimestamp >= invocationThreshold)
            {
                this._logger.LogWarning($"Not going to process job for app owner #{appOwnerId}: app owner group has already been sent a digest (possible race condition?)");
                return;
            }

            var users = await this._appUserManager.Users.Where(u => u.AvailableGroups.Any(g => g.GroupId == appOwnerId) && u.Preferences.EnableMonthlyDigest).ToListAsync();

            if (users.Count == 0)
            {
                this._logger.LogInformation($"Not going to process job for app owner #{appOwnerId}: no users want to receive the monthly digest");
                return;
            }

            this._logger.LogInformation($"Starting job for app owner #{appOwnerId}: Sent digest to {users.Count} users");

            await this.SendDigestInternal(appOwner, users);

            this._logger.LogInformation($"Completed job for app owner #{appOwnerId}: Sent digest to {users.Count} users");

            appOwner.LastMonthlyDigestTimestamp = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
            await this._appOwnerRepository.SaveChangesAsync();
        }
コード例 #11
0
        public async Task ChangeActiveGroupAsync(ClaimsPrincipal currentPrincipal, AppUser currentUser, AppOwner newGroup, HttpContext httpContext)
        {
            // Determine active owned group (before impersonation)
            int?currentOwnedGroupId = currentPrincipal.Identity.GetPreviousActiveOwnedOwnerGroupId() ?? currentUser.AvailableGroups.FirstOrDefault(x => x.GroupId == currentUser.CurrentGroupId && x.HasOwnership)?.GroupId;

            if (currentOwnedGroupId == null)
            {
                currentOwnedGroupId = currentUser.AvailableGroups.First(x => x.HasOwnership).GroupId;
            }

            // Change the group and save
            currentUser.CurrentGroup   = newGroup;
            currentUser.CurrentGroupId = newGroup.Id;

            await this._appUserManager.UpdateAsync(currentUser);

            // Send new login token
            var identity = ((ClaimsIdentity)currentPrincipal.Identity);

            identity.TryRemoveClaim(identity.FindFirst(AppClaimTypes.AppOwnerGroup));
            identity.AddClaim(new Claim(AppClaimTypes.AppOwnerGroup, currentUser.CurrentGroupId.ToString(CultureInfo.InvariantCulture)));

            identity.TryRemoveClaim(identity.FindFirst(AppClaimTypes.PreviousActiveOwnedAppOwnerGroup));

            if (currentUser.AvailableGroups.Any(x => x.HasOwnership == false && x.GroupId == newGroup.Id))
            {
                identity.AddClaim(new Claim(AppClaimTypes.PreviousActiveOwnedAppOwnerGroup, currentOwnedGroupId.Value.ToString(CultureInfo.InvariantCulture)));
            }

            await httpContext.SignInAsync(IdentityConstants.ApplicationScheme, currentPrincipal);
        }
コード例 #12
0
 public void Add(AppOwner item)
 {
     this._entitySet.Add(item);
 }