Esempio n. 1
0
        private void DeleteUser(DtDelta delta)
        {
            var adusername = sqlService.GetUserForId(delta.DataForDelete).AdUsername;

            adservice.DeleteUser(adusername);
            sqlService.RemoveUserRecord(delta.DataForDelete);
        }
Esempio n. 2
0
        private void DeleteGroup(DtDelta delta)
        {
            var username = sqlService.GetGroupnameForId(delta.DataForDelete);

            adservice.DeleteGroup(username);
            sqlService.RemoveGroupRecord(username);
        }
Esempio n. 3
0
 private void InsertDedupeDeltaAction(DtDelta rootDelta, DtDelta latestDelta, List <DeltaAction> result)
 {
     if (rootDelta.Seq == latestDelta.Seq)
     {
         //if rootDelta == latestDelta, then there are no later sequenced deltas.
         //but there may be prior ones in the results.
         //  check if rootDelta exists as new delta. if not, then add
         if (!result.Exists(d => d.NewDelta.Seq == rootDelta.Seq))
         {
             result.Add(new DeltaAction(rootDelta, latestDelta));
         }
         //else can ignore, as will be picked up by resolver
     }
     else
     {
         if (result.Exists(d => d.NewDelta.Seq == latestDelta.Seq))
         {
             //if latestDelta exists as new delta
             //then old delta should be identical. check, then set oldest as rootDelta.
             var existing = result.First(d => d.NewDelta.Seq == latestDelta.Seq);
             if (existing.OldDelta.Seq > rootDelta.Seq)
             {
                 result.Remove(existing);
                 result.Add(new DeltaAction(rootDelta, latestDelta));
             }
         }
         else
         {
             result.Add(new DeltaAction(rootDelta, latestDelta));
         }
     }
 }
Esempio n. 4
0
        private List <DeltaAction> PairGroupDeltas(IEnumerable <DtDelta> deltas)
        {
            var inserts = deltas.Where(d => !d.IsDelete);
            var deletes = deltas.Where(d => d.IsDelete);
            var result  = new List <DeltaAction>(); //old values, new values

            foreach (var rootDelta in deltas)
            {
                //this is the root entry
                //recurse through other entries to find final related entry
                var foundEnd    = false;
                var latestDelta = rootDelta;
                do
                {
                    DtDelta temp = null;
                    temp = FindNextRelatedGroupDelta(latestDelta, inserts, deletes);

                    if (temp != null)
                    {
                        latestDelta = temp;
                    }
                    else
                    {
                        foundEnd = true;
                    }
                } while (!foundEnd);

                InsertDedupeDeltaAction(rootDelta, latestDelta, result);
            }

            return(result);
        }
Esempio n. 5
0
        private void CreateUser(DtDelta delta)
        {
            var user = delta.DataAsUser;

            adservice.CreateUser(ref user);
            sqlService.CreateUserRecord(user);
            //removed autoemail
            //emailservice.SendWelcomeEmail(user);
        }
Esempio n. 6
0
        private void UpdateUser(DtDelta delta)
        {
            var userData  = delta.DataAsUser;
            var savedUser = sqlService.GetUserForId(userData.Id);

            userData.AdUsername = savedUser.AdUsername;
            if (adservice.UpdateUser(delta.DataAsUser))
            {
                sqlService.UpdateUserRecord(userData);
            }
        }
Esempio n. 7
0
        private DtDelta FindNextRelatedGroupDelta(DtDelta rootDtDelta, IEnumerable <DtDelta> allInserts, IEnumerable <DtDelta> allDeletes)
        {
            DtDelta result = null;

            if (rootDtDelta.Action == DtDelta.DeleteAction)
            {
                //get username for this deletion id
                var groupnameToDelete = string.Empty;
                var earlierInsert     = allInserts.
                                        FirstOrDefault(i => i.Seq < rootDtDelta.Seq &&
                                                       i.DataAsGroup.Id == rootDtDelta.DataForDelete);

                if (earlierInsert != null)
                {
                    groupnameToDelete = earlierInsert.DataAsGroup.Name;
                }

                //if there's no value in the array, there may be one in the db
                if (string.IsNullOrWhiteSpace(groupnameToDelete))
                {
                    groupnameToDelete = sqlService.GetGroupnameForId(rootDtDelta.DataForDelete);
                }

                //if you can't find a record for this Id, you can't proceed. Something is wrong with data.
                if (string.IsNullOrWhiteSpace(groupnameToDelete))
                {
                    throw new Exception(string.Format("No role insert record found for this id: {0}", rootDtDelta.DataForDelete));
                }

                //search for next insert for same username as delete
                var insertsForGroupname = allInserts.
                                          Where(i => i.Seq > rootDtDelta.Seq &&
                                                i.DataAsGroup.Name == groupnameToDelete)
                                          .OrderBy(a => a.Seq);
                if (insertsForGroupname.Any())
                {
                    result = insertsForGroupname.First();
                }
            }
            else if (rootDtDelta.Action == DtDelta.InsertAction)
            {
                //search for delete action for same id
                var deletesForId = allDeletes.
                                   Where(i => i.Seq > rootDtDelta.Seq &&
                                         i.DataForDelete == rootDtDelta.DataAsGroup.Id);
                if (deletesForId.Any())
                {
                    result = deletesForId.First();
                }
            }

            return(result);
        }
Esempio n. 8
0
        private DtDelta FindSubsequentUserRoleDeletion(DtDelta rootDtDelta, IEnumerable <DtDelta> allDeletes)
        {
            DtDelta result = null;

            if (!rootDtDelta.IsDelete)
            {
                //search for delete action for same id
                result = allDeletes.
                         FirstOrDefault(i => i.Seq > rootDtDelta.Seq &&
                                        i.DataForDelete == rootDtDelta.DataAsUserGroup.Id);
            }

            return(result);
        }
Esempio n. 9
0
        public IEnumerable <DtDelta> GetDeltas(string documentName, int currentDelta)
        {
            var offset  = 0;
            var hasMore = true;
            var result  = new List <DtDelta>();

            do
            {
                var docString = client.GetDelta(documentName, offset, ReadLimit, currentDelta);
                var jarray    = JsonConvert.DeserializeObject <JArray>(docString);

                if (jarray == null)
                {
                    hasMore = false;
                    continue;
                }

                if (jarray.Count == 1)
                {
                    if (jarray[0].Type != JTokenType.Object)
                    {
                        hasMore = false;
                        continue;
                    }
                }


                foreach (var delta in jarray)
                {
                    var datowniaDelta = new DtDelta().FromContentRow(delta);
                    result.Add(datowniaDelta);
                }

                if (jarray.Count < ReadLimit)
                {
                    hasMore = false;
                }
                else
                {
                    offset += ReadLimit;
                }
            } while (hasMore);

            return(result);
        }
Esempio n. 10
0
        private List <DtDelta> RemoveRedundantDeltas(IEnumerable <DtDelta> deltas)
        {
            //normally, we follow the chain of alternate deletes and inserts
            //resulting in:
            //a delete then insert => this is an update
            //or a delete then delete (via one or more inserts) => this is a delete of the old value
            //or an insert then delete => cancels out
            //or an insert then insert (via one or more deletes) => only insert the latest value

            //BUT
            //in this case, we need to do this:
            //if it's a delete we can't check for subsequent inserts, due to compound key.
            //  we cannot find a chain. this means we process the delete on AD individually.
            //if it's an insert, we can look for a corresponding deletion and ignore both
            //  if no deletion is found, then we're looking at a standalone insertion. process as such.

            var deletes = deltas.Where(d => d.IsDelete);
            var result  = new List <DtDelta>(); //old values, new values

            foreach (var rootDelta in deltas)
            {
                DtDelta nextDelta = null;

                if (rootDelta.IsDelete)
                {
                    //it is a delete.
                    result.Add(rootDelta);
                }
                else
                {
                    nextDelta = FindSubsequentUserRoleDeletion(rootDelta, deletes);
                    if (nextDelta == null)
                    {
                        //data is an insert.
                        result.Add(rootDelta);
                    }
                    //else the insert gets deleted so can be ignored
                }
            }

            return(result);
        }
Esempio n. 11
0
        private void ResolveUserGroupDelta(DtDelta action)
        {
            if (action.IsDelete)
            {
                //remove from AD
                var userRole = sqlService.GetUserGroupForId(action.DataForDelete);
                if (userRole != null)
                {
                    var user = sqlService.GetUserByUsername(userRole.Username);
                    if (user != null)
                    {
                        var adUsername = user.AdUsername;
                        adservice.RemoveUserGroup(adUsername, userRole.Group);
                    }

                    //remove from sql
                    sqlService.RemoveUserGroupRecord(action.DataForDelete);
                }
            }
            else
            {
                //add to AD
                var userRole       = action.DataAsUserGroup;
                var userByUsername = sqlService.GetUserByUsername(userRole.Username);
                if (userByUsername == null)
                {
                    Trace.WriteLine("Could not find user related to group");
                    return;
                }

                var adUsername = userByUsername.AdUsername;
                adservice.AddUserGroup(adUsername, userRole.Group);

                //insert into sql
                sqlService.CreateUserGroupRecord(userRole);
            }
        }
Esempio n. 12
0
 private void CreateGroup(DtDelta delta)
 {
     adservice.CreateGroup(delta.DataAsGroup);
     sqlService.CreateGroupRecord(delta.DataAsGroup);
 }