Exemplo n.º 1
0
        public void EditeUserInOrgAuth(UserDetailes user, UserContext db)
        {
            UserInOrg uio = null;

            if (user.UserInOrgId > 0)
            {
                uio = db.UserInOrg.FirstOrDefault(x => x.Id == user.UserInOrgId);
            }


            if (uio != null)
            {
                if (!AuthTypes.checkTypeExistence(user.Auth))
                {
                    throw new Exception("");
                }
                //uio.Auth.ToList().ForEach(x => uio.Auth.Remove(x));
                //db.SaveChanges();
                foreach (Auth auth in uio.Auth.ToList())
                {
                    db.Auth.Remove(auth);
                }
                db.SaveChanges();
                user.Auth.ForEach(x => uio.Auth.Add(new Auth {
                    Type = x
                }));
                db.SaveChanges();
            }
        }
 //Edit Auth To Dep ----- I Samma Org
 public void AddEditAuthToDep(DepDetailes AuthDep, UserContext db)
 {
     if (!AuthTypes.checkTypeExistence(AuthDep.AuthType.ToList()))
     {
         throw new Exception("the authentication not exist");
     }
     foreach (Auth auth in db.Auth.Where(x => x.departmentId == AuthDep.Id).ToList())
     {
         db.Auth.Remove(auth);
         db.SaveChanges();
     }
     foreach (string typeA in AuthDep.AuthType.ToList())
     {
         db.Auth.Add(new Auth {
             departmentId = AuthDep.Id, Type = typeA
         });
         db.SaveChanges();
     }
 }
        //Add Department + Parent
        public void AddDepartment(DepDetailes dep, UserContext db)
        {
            if (!AuthTypes.checkTypeExistence(dep.AuthType))
            {
                throw new Exception("The AuthType Not Valid");
            }
            if (dep.parentId > 0 && db.department.FirstOrDefault(x => x.Id == dep.parentId) != null)
            {
                db.department.Add(new department {
                    Name = dep.Name, OrgId = dep.OrgId
                });
                db.SaveChanges();

                department ch = db.department.First(x => x.Name == dep.Name && x.OrgId == dep.OrgId);
                db.DepPointer.Add(new DepPointer {
                    ChildId = ch.Id, ParentId = dep.parentId
                });
                db.SaveChanges();

                dep.AuthType.ForEach(x => db.Auth.Add(new Auth {
                    departmentId = ch.Id, Type = x
                }));
                db.SaveChanges();
            }
            else if (dep.parentId > 0 && db.department.FirstOrDefault(x => x.Id == dep.parentId) == null)
            {
                throw new Exception("this parent is not registred");
            }

            if (dep.parentId == 0)
            {
                db.department.Add(new department {
                    Name = dep.Name, OrgId = dep.OrgId
                });
                db.SaveChanges();
                department ch = db.department.First(x => x.Name == dep.Name && x.OrgId == dep.OrgId);
                dep.AuthType.ForEach(x => db.Auth.Add(new Auth {
                    departmentId = ch.Id, Type = x
                }));
                db.SaveChanges();
            }
        }
        //Edit Dep ----- I Samma Org
        public void EditDepartment(DepDetailes dep, UserContext db)
        {
            if (!AuthTypes.checkTypeExistence(dep.AuthType))
            {
                throw new Exception("this Authentication type is not exist");
            }
            department Dep = db.department.First(x => x.Id == dep.Id);

            Dep.Name    = dep.Name;
            Dep.AdminId = dep.AdminId;

            db.Entry(Dep).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();
            db.department.Find(dep.Id).Auth.ToList().ForEach(x => db.Auth.Remove(x));
            db.SaveChanges();
            dep.AuthType.ForEach(x => db.department.Find(dep.Id).Auth.Add(new Auth {
                Type = x
            }));
            db.SaveChanges();
        }