Beispiel #1
0
        public async Task ListenForEvents(IEvent @event)
        {
            PermissionDetailProjection perm = new PermissionDetailProjection();

            switch (@event)
            {
            case PermissionCreatedEvent pc:
                perm.PermissionId = pc.Id;
                perm.Name         = pc.Name;
                perm.Description  = pc.Description;
                perm.IsActive     = pc.IsActive;
                perm.DateCreated  = pc.TimeStamp;
                perm.Image        = pc.Image;

                await _context.PermissionDetailProjection.AddAsync(perm);

                break;

            case PermissionInfoUpdatedEvent pu:
                perm = await _context.PermissionDetailProjection.SingleAsync(a => a.PermissionId == pu.Id);

                _context.Attach(perm);

                perm.Name        = pu.Name;
                perm.Description = pu.Description;
                perm.Image       = pu.Image;
                _context.Entry(perm).Property(a => a.Name).IsModified        = true;
                _context.Entry(perm).Property(a => a.Description).IsModified = true;
                _context.Entry(perm).Property(a => a.Image).IsModified       = true;

                _context.PermissionDetailProjection.Update(perm);
                break;

            case PermissionDisabledEvent pd:
                perm = await _context.PermissionDetailProjection.SingleAsync(a => a.PermissionId == pd.Id);

                _context.Attach(perm);

                perm.IsActive = pd.IsActive;
                _context.Entry(perm).Property(a => a.IsActive).IsModified = true;

                _context.PermissionDetailProjection.Update(perm);
                break;

            case PermissionEnabledEvent pe:
                perm = await _context.PermissionDetailProjection.SingleAsync(a => a.PermissionId == pe.Id);

                _context.Attach(perm);

                perm.IsActive = pe.IsActive;
                _context.Entry(perm).Property(a => a.IsActive).IsModified = true;

                _context.PermissionDetailProjection.Update(perm);
                break;
            }

            await _context.SaveChangesAsync();
        }
Beispiel #2
0
        public async Task ListenForEvents(IEvent @event)
        {
            UserDetailProjection user = new UserDetailProjection();
            Dictionary <Guid, DetailedUserPermissionInfo> permissionList =
                new Dictionary <Guid, DetailedUserPermissionInfo>();

            switch (@event)
            {
            case UserCreatedEvent uc:
                user.UserId     = uc.Id;
                user.FirstName  = uc.FirstName;
                user.LastName   = uc.LastName;
                user.Email      = uc.Email;
                user.Position   = uc.Position;
                user.Department = uc.Department;
                user.IsActive   = uc.IsActive;
                user.IsAdmin    = uc.IsAdmin;
                if (uc.PermissionList != null)
                {
                    foreach (var permission in uc.PermissionList)
                    {
                        var userDetail = await _context.UserDetailProjection
                                         .Where(a => a.UserId == permission.Value.RequestedBy).SingleAsync();

                        permissionList.Add(permission.Key,
                                           new DetailedUserPermissionInfo
                        {
                            PermissionName =
                                await _context.PermissionDetailProjection
                                .Where(a => a.PermissionId == permission.Key).Select(p => p.Name)
                                .SingleAsync(),
                            ApproverFirstName = userDetail.FirstName,
                            ApproverLastName  = userDetail.LastName,
                            PermissionStatus  = "Granted",
                        });
                    }
                }

                user.PermissionList = JsonConvert.SerializeObject(permissionList);
                user.DateCreated    = uc.TimeStamp;
                _context.UserDetailProjection.Add(user);
                break;

            case UserUpdatedEvent uu:
                user = await _context.UserDetailProjection.SingleAsync(b => b.UserId == uu.Id);

                _context.UserDetailProjection.Attach(user);
                user.FirstName  = uu.FirstName;
                user.LastName   = uu.LastName;
                user.Email      = uu.Email;
                user.Position   = uu.Position;
                user.Department = uu.Department;
                user.IsAdmin    = uu.IsAdmin;
                _context.Entry(user).Property(a => a.FirstName).IsModified  = true;
                _context.Entry(user).Property(a => a.LastName).IsModified   = true;
                _context.Entry(user).Property(a => a.Email).IsModified      = true;
                _context.Entry(user).Property(a => a.Position).IsModified   = true;
                _context.Entry(user).Property(a => a.Department).IsModified = true;
                _context.Entry(user).Property(a => a.IsAdmin).IsModified    = true;
                _context.UserDetailProjection.Update(user);
                break;

            case UserDisabledEvent ud:
                user = await _context.UserDetailProjection.SingleAsync(b => b.UserId == ud.Id);

                _context.UserDetailProjection.Attach(user);
                user.IsActive = ud.IsActive;
                _context.Entry(user).Property(a => a.IsActive).IsModified = true;
                _context.UserDetailProjection.Update(user);
                break;

            case UserEnabledEvent ue:
                user = await _context.UserDetailProjection.SingleAsync(b => b.UserId == ue.Id);

                _context.UserDetailProjection.Attach(user);
                user.IsActive = ue.IsActive;
                _context.Entry(user).Property(a => a.IsActive).IsModified = true;
                _context.UserDetailProjection.Update(user);
                break;

            case UserPermissionsRequestedEvent upr:
                user = await _context.UserDetailProjection.SingleAsync(b => b.UserId == upr.Id);

                _context.UserDetailProjection.Attach(user);
                permissionList =
                    JsonConvert.DeserializeObject <Dictionary <Guid, DetailedUserPermissionInfo> >(
                        user.PermissionList);
                foreach (var permission in upr.Requests)
                {
                    if (!permissionList.ContainsKey(permission.Key))
                    {
                        var userDetail = await _context.UserDetailProjection
                                         .Where(a => a.UserId == permission.Value.RequestedBy).SingleAsync();

                        permissionList.Add(permission.Key,
                                           new DetailedUserPermissionInfo
                        {
                            PermissionName =
                                await _context.PermissionDetailProjection
                                .Where(a => a.PermissionId == permission.Key).Select(p => p.Name)
                                .SingleAsync(),
                            ApproverFirstName = userDetail.FirstName,
                            ApproverLastName  = userDetail.LastName,
                            PermissionStatus  = "Requested",
                            TimeStamp         = upr.TimeStamp,
                        });
                    }
                    else
                    {
                        permissionList[permission.Key].PermissionStatus = "Requested";
                        permissionList[permission.Key].TimeStamp        = upr.TimeStamp;
                    }
                }

                user.PermissionList = JsonConvert.SerializeObject(permissionList);
                _context.Entry(user).Property(a => a.PermissionList).IsModified = true;
                _context.UserDetailProjection.Update(user);
                break;

            case UserPermissionRequestDeniedEvent pde:
                if (pde.PermissionsToDeny.Any())
                {
                    user = await _context.UserDetailProjection.SingleAsync(a => a.UserId == pde.ForId);

                    _context.UserDetailProjection.Attach(user);
                    permissionList =
                        JsonConvert.DeserializeObject <Dictionary <Guid, DetailedUserPermissionInfo> >(
                            user.PermissionList);
                    foreach (var permission in pde.PermissionsToDeny)
                    {
                        //Cannot Deny a permission that has not been Requested or has already been Granted
                        if (permissionList.ContainsKey(permission.Key))
                        {
                            permissionList[permission.Key].PermissionStatus = "Denied";
                            permissionList[permission.Key].TimeStamp        = pde.TimeStamp;
                        }
                    }

                    user.PermissionList = JsonConvert.SerializeObject(permissionList);
                    _context.Entry(user).Property(a => a.PermissionList).IsModified = true;
                    _context.UserDetailProjection.Update(user);
                }
                break;

            case UserPermissionGrantedEvent pg:
                if (pg.PermissionsToGrant.Any())
                {
                    user = await _context.UserDetailProjection.SingleAsync(a => a.UserId == pg.Id);

                    _context.UserDetailProjection.Attach(user);
                    permissionList =
                        JsonConvert.DeserializeObject <Dictionary <Guid, DetailedUserPermissionInfo> >(
                            user.PermissionList);
                    foreach (var p in pg.PermissionsToGrant)
                    {
                        if (permissionList.ContainsKey(p.Key))
                        {
                            permissionList[p.Key].PermissionStatus = "Granted";
                            permissionList[p.Key].TimeStamp        = pg.TimeStamp;
                        }
                        else
                        {
                            var userDetail = await _context.UserDetailProjection
                                             .Where(a => a.UserId == p.Value.RequestedBy).SingleAsync();

                            permissionList.Add(p.Key,
                                               new DetailedUserPermissionInfo
                            {
                                PermissionName =
                                    await _context.PermissionDetailProjection
                                    .Where(a => a.PermissionId == p.Key).Select(b => b.Name)
                                    .SingleAsync(),
                                ApproverFirstName = userDetail.FirstName,
                                ApproverLastName  = userDetail.LastName,
                                PermissionStatus  = "Granted",
                                TimeStamp         = pg.TimeStamp,
                            });
                        }
                    }
                    user.PermissionList = JsonConvert.SerializeObject(permissionList);
                    _context.Entry(user).Property(c => c.PermissionList).IsModified = true;
                    _context.UserDetailProjection.Update(user);
                }
                break;

            case UserPermissionRevokedEvent pr:
                if (pr.PermissionsToRevoke.Any())
                {
                    user = await _context.UserDetailProjection.SingleAsync(a => a.UserId == pr.Id);

                    _context.UserDetailProjection.Attach(user);
                    permissionList =
                        JsonConvert.DeserializeObject <Dictionary <Guid, DetailedUserPermissionInfo> >(
                            user.PermissionList);
                    foreach (var permission in pr.PermissionsToRevoke)
                    {
                        //Cannot Deny a permission that has not been Granted
                        if (permissionList.ContainsKey(permission.Key))
                        {
                            permissionList[permission.Key].PermissionStatus = "Revoked";
                            permissionList[permission.Key].TimeStamp        = pr.TimeStamp;
                        }
                    }

                    user.PermissionList = JsonConvert.SerializeObject(permissionList);
                    _context.Entry(user).Property(a => a.PermissionList).IsModified = true;
                    _context.UserDetailProjection.Update(user);
                }
                break;

            case PermissionInfoUpdatedEvent pInfoUpdatedEvent:
                if (pInfoUpdatedEvent.Name.Any())
                {
                    var aAlist = _context.UserDetailProjection.Where(p =>
                                                                     JsonConvert
                                                                     .DeserializeObject <Dictionary <Guid, DetailedUserPermissionInfo> >(p.PermissionList)
                                                                     .ContainsKey(pInfoUpdatedEvent.Id)).ToList();
                    var iDToCheck = pInfoUpdatedEvent.Id;
                    var newName   = pInfoUpdatedEvent.Name;
                    foreach (var aActivity in aAlist)
                    {
                        //_context.Entry(aActivity).State = EntityState.Modified;
                        permissionList =
                            JsonConvert.DeserializeObject <Dictionary <Guid, DetailedUserPermissionInfo> >(
                                aActivity.PermissionList);
                        permissionList[iDToCheck].PermissionName = newName;
                        aActivity.PermissionList = JsonConvert.SerializeObject(permissionList);

                        _context.UserDetailProjection.Update(aActivity);
                    }
                }
                break;
            }
            await _context.SaveChangesAsync();
        }
Beispiel #3
0
        public async Task ListenForEvents(IEvent @event)
        {
            DashboardProjection dBProjection = new DashboardProjection();

            switch (@event)
            {
            case PermissionInfoUpdatedEvent pue:
                dBProjection = await _context.DashBoardProjection.SingleAsync(a => a.PermissionId == pue.Id);

                _context.Attach(dBProjection);

                dBProjection.SystemName = pue.Name;
                dBProjection.LogoUrl    = pue.Image;

                _context.Entry(dBProjection).Property(a => a.SystemName).IsModified = true;
                _context.Entry(dBProjection).Property(a => a.LogoUrl).IsModified    = true;

                _context.DashBoardProjection.Update(dBProjection);
                break;

            case PermissionCreatedEvent pce:
                dBProjection.PermissionId    = pce.Id;
                dBProjection.SystemName      = pce.Name;
                dBProjection.ActiveUsers     = 0;
                dBProjection.PendingRequests = 0;
                dBProjection.LogoUrl         = pce.Image;
                _context.DashBoardProjection.Add(dBProjection);
                break;

            case UserPermissionsRequestedEvent pre:
                //increment pending requests
                var rowsToBeUpdated = await _context.DashBoardProjection.Where(a => pre.Requests.ContainsKey(a.PermissionId)).ToListAsync();

                foreach (var entry in rowsToBeUpdated)
                {
                    entry.PendingRequests++;

                    _context.Entry(entry).Property(a => a.PendingRequests).IsModified = true;
                    _context.Update(entry);
                }
                break;

            case UserPermissionRequestDeniedEvent prd:
                //decrement pending requests
                var rows = await _context.DashBoardProjection.Where(a => prd.PermissionsToDeny.ContainsKey(a.PermissionId)).ToListAsync();

                foreach (var entry in rows)
                {
                    if (entry.PendingRequests <= 1)
                    {
                        entry.PendingRequests = 0;
                    }
                    else
                    {
                        entry.PendingRequests--;
                    }
                    _context.Entry(entry).Property(a => a.PendingRequests).IsModified = true;
                    _context.Update(entry);
                }
                break;

            case UserPermissionGrantedEvent pge:
                //increment active users, decrement pending requests
                var rs = await _context.DashBoardProjection.Where(a => pge.PermissionsToGrant.ContainsKey(a.PermissionId)).ToListAsync();

                var user = await _context.UserDetailProjection.Where(a => a.UserId == pge.ForId).SingleOrDefaultAsync();

                var permissionList =
                    JsonConvert.DeserializeObject <Dictionary <Guid, DetailedUserPermissionInfo> >(
                        user.PermissionList);

                foreach (var entry in rs)
                {
                    entry.ActiveUsers++;
                    _context.Entry(entry).Property(a => a.ActiveUsers).IsModified = true;

                    //no need to go further because their list did not contain a definition for this permission meaning it was never previously requested.
                    if (!permissionList.ContainsKey(entry.PermissionId))
                    {
                        _context.Update(entry);
                        break;
                    }

                    // checks previous state of that permission
                    if (string.Equals(permissionList[entry.PermissionId].PermissionStatus, "Requested"))
                    {
                        if (entry.PendingRequests <= 1)
                        {
                            entry.PendingRequests = 0;
                        }
                        else
                        {
                            entry.PendingRequests--;
                        }
                    }
                    _context.Entry(entry).Property(a => a.PendingRequests).IsModified = true;
                    _context.Update(entry);
                }
                break;

            case UserPermissionRevokedEvent upr:
                //decrement active users
                var r = await _context.DashBoardProjection.Where(a => upr.PermissionsToRevoke.ContainsKey(a.PermissionId)).ToListAsync();

                foreach (var entry in r)
                {
                    if (entry.ActiveUsers <= 1)
                    {
                        entry.ActiveUsers = 0;
                    }
                    else
                    {
                        entry.ActiveUsers--;
                    }

                    _context.Entry(entry).Property(a => a.ActiveUsers).IsModified = true;
                    _context.Update(entry);
                }
                break;

            case UserCreatedEvent uc:
                if (uc.PermissionList.Any())
                {
                    var rowsToChange = await _context.DashBoardProjection.Where(a => uc.PermissionList.ContainsKey(a.PermissionId)).ToListAsync();

                    foreach (var row in rowsToChange)
                    {
                        row.ActiveUsers++;
                    }
                }
                break;
            }
            await _context.SaveChangesAsync();
        }
Beispiel #4
0
        public async Task ListenForEvents(IEvent @event)
        {
            SystemActivityProjection proj = new SystemActivityProjection();

            switch (@event)
            {
            case UserPermissionGrantedEvent pg:
                foreach (var permission in pg.PermissionsToGrant)
                {
                    //US-075
                    var newGrantedRow = new SystemActivityProjection
                    {
                        ForId          = pg.ForId,
                        ById           = pg.ById,
                        PermissionId   = permission.Key,
                        EventType      = "Permission Granted",
                        Timestamp      = pg.TimeStamp,
                        PermissionName = await _context.PermissionDetailProjection.Where(a => a.PermissionId == permission.Key).Select(p => p.Name).SingleOrDefaultAsync(),
                        ForFullName    = await _context.UserDetailProjection.Where(a => a.UserId == pg.ForId).Select(p => p.FirstName + " " + p.LastName).SingleOrDefaultAsync(),
                        ByFullName     = await _context.UserDetailProjection.Where(a => a.UserId == pg.ById).Select(p => p.FirstName + " " + p.LastName).SingleOrDefaultAsync()
                    };

                    _context.SystemActivityProjection.Add(newGrantedRow);
                }
                break;

            case UserPermissionRevokedEvent pr:
                foreach (var permission in pr.PermissionsToRevoke)
                {
                    var newRevokedRow = new SystemActivityProjection
                    {
                        ForId          = pr.ForId,
                        ById           = pr.ById,
                        PermissionId   = permission.Key,
                        EventType      = "Permission Revoked",
                        Timestamp      = pr.TimeStamp,
                        PermissionName = await _context.PermissionDetailProjection.Where(a => a.PermissionId == permission.Key).Select(p => p.Name).SingleOrDefaultAsync(),
                        ForFullName    = await _context.UserDetailProjection.Where(a => a.UserId == pr.ForId).Select(p => p.FirstName + " " + p.LastName).SingleOrDefaultAsync()
                    };
                    if (pr.ById != Guid.Empty)
                    {
                        newRevokedRow.ByFullName = await _context.UserDetailProjection.Where(a => a.UserId == pr.ById).Select(p => p.FirstName + " " + p.LastName).SingleOrDefaultAsync();
                    }
                    else
                    {
                        newRevokedRow.ByFullName = "System";
                    }

                    await _context.SystemActivityProjection.AddAsync(newRevokedRow);
                }
                break;

            case UserUpdatedEvent uu:
                var entries = await _context.SystemActivityProjection.Where(a => a.ForId == uu.Id || a.ById == uu.Id).ToListAsync();

                if (entries.Any())
                {
                    foreach (var item in entries)
                    {
                        _context.SystemActivityProjection.Attach(item);

                        if (item.ById == uu.Id)
                        {
                            item.ByFullName = uu.FirstName + " " + uu.LastName;
                            _context.Entry(item).Property(a => a.ByFullName).IsModified = true;
                        }

                        if (item.ForId == uu.Id)
                        {
                            item.ForFullName = uu.FirstName + " " + uu.LastName;
                            _context.Entry(item).Property(a => a.ForFullName).IsModified = true;
                        }

                        _context.SystemActivityProjection.Update(item);
                    }
                }
                break;

            case UserCreatedEvent uc:
                foreach (var permission in uc.PermissionList)
                {
                    //US-075
                    var newGrantedRow = new SystemActivityProjection
                    {
                        ForId          = uc.Id,
                        ById           = uc.ById,
                        PermissionId   = permission.Key,
                        EventType      = "Permission Granted",
                        Timestamp      = uc.TimeStamp,
                        PermissionName = await _context.PermissionDetailProjection.Where(a => a.PermissionId == permission.Key).Select(p => p.Name).SingleOrDefaultAsync(),
                        ForFullName    = await _context.UserDetailProjection.Where(a => a.UserId == uc.Id).Select(p => p.FirstName + " " + p.LastName).SingleOrDefaultAsync(),
                        ByFullName     = await _context.UserDetailProjection.Where(a => a.UserId == uc.ById).Select(p => p.FirstName + " " + p.LastName).SingleOrDefaultAsync()
                    };

                    _context.SystemActivityProjection.Add(newGrantedRow);
                }
                break;

            case PermissionInfoUpdatedEvent pi:
                var rows = await _context.SystemActivityProjection.Where(a => a.PermissionId == pi.Id).ToListAsync();

                if (rows.Any())
                {
                    foreach (var row in rows)
                    {
                        row.PermissionName = pi.Name;

                        _context.Entry(row).Property(a => a.PermissionName).IsModified = true;
                        _context.SystemActivityProjection.Update(row);
                    }
                }
                break;
            }

            await _context.SaveChangesAsync();
        }
Beispiel #5
0
        public async Task ListenForEvents(IEvent @event)
        {
            UserManagementProjection user = new UserManagementProjection();

            switch (@event)
            {
            case UserCreatedEvent uc:
                user.UserId     = uc.Id;
                user.FirstName  = uc.FirstName;
                user.LastName   = uc.LastName;
                user.Email      = uc.Email;
                user.Position   = uc.Position;
                user.Department = uc.Department;
                user.IsActive   = uc.IsActive;
                user.IsAdmin    = uc.IsAdmin;

                _context.UserManagementProjection.Add(user);
                break;

            case UserUpdatedEvent uu:
                user = await _context.UserManagementProjection.SingleAsync(b => b.UserId == uu.Id);

                _context.UserManagementProjection.Attach(user);

                user.FirstName  = uu.FirstName;
                user.LastName   = uu.LastName;
                user.Email      = uu.Email;
                user.Position   = uu.Position;
                user.Department = uu.Department;
                user.IsAdmin    = uu.IsAdmin;
                _context.Entry(user).Property(a => a.FirstName).IsModified  = true;
                _context.Entry(user).Property(a => a.LastName).IsModified   = true;
                _context.Entry(user).Property(a => a.Email).IsModified      = true;
                _context.Entry(user).Property(a => a.Position).IsModified   = true;
                _context.Entry(user).Property(a => a.Department).IsModified = true;
                _context.Entry(user).Property(a => a.IsAdmin).IsModified    = true;

                _context.UserManagementProjection.Update(user);
                break;

            case UserDisabledEvent ud:
                user = await _context.UserManagementProjection.SingleAsync(b => b.UserId == ud.Id);

                _context.UserManagementProjection.Attach(user);

                user.IsActive = ud.IsActive;
                _context.Entry(user).Property(a => a.IsActive).IsModified = true;

                _context.UserManagementProjection.Update(user);
                break;

            case UserEnabledEvent ue:
                user = await _context.UserManagementProjection.SingleAsync(b => b.UserId == ue.Id);

                _context.UserManagementProjection.Attach(user);

                user.IsActive = ue.IsActive;
                _context.Entry(user).Property(a => a.IsActive).IsModified = true;

                _context.UserManagementProjection.Update(user);
                break;
            }

            await _context.SaveChangesAsync();
        }