Esempio n. 1
0
        public bool DeleteUser(string login, bool deleteAllRelatedData)
        {
            using (RepositoryEntities db = new RepositoryEntities())
            {
                var user = db.Users.Where(p => p.UserLogin == login);

                if (user.Count() == 0)
                {
                    return(false);
                }
                int id = user.Select(p => p.ID).First();
                db.UserRoles.Where(w => w.UserId == id).ForEach(x => db.DeleteObject(x));

                if (db.Contracts.Where(p => p.UserId == id).Count() > 0)
                {
                    if (deleteAllRelatedData)
                    {
                        db.Contracts.Where(p => p.UserId == id).ForEach(x => db.DeleteObject(x));
                    }
                    else
                    {
                        db.Users.Where(p => p.UserLogin == login).First().UserPsw = "";
                    }
                }
                else
                {
                    db.DeleteObject(db.Users.Where(p => p.UserLogin == login).First());
                }
                db.SaveChanges();
            }
            return(true);
        }
Esempio n. 2
0
 public void Delete(int id)
 {
     using (RepositoryEntities db = new RepositoryEntities())
     {
         db.DeleteObject(db.Contracts.Where(p => p.ID == id).First());
         db.SaveChanges();
     }
 }
Esempio n. 3
0
 public void Update(int id)
 {
     using (RepositoryEntities db = new RepositoryEntities())
     {
         db.Contracts.Where(p => p.ID == id).ForEach(x => x.Status = true);
         db.SaveChanges();
     }
 }
Esempio n. 4
0
        public bool CreateUser(string login, string psw, string role)
        {
            using (RepositoryEntities db = new RepositoryEntities())
            {
                int roleId = db.Roles.Where(p => p.Name == role).Select(p => p.Id).First();

                Users newUser = new Users()
                {
                    UserLogin = login,
                    UserPsw   = GetHash(psw)
                };
                var us = db.Users.Where(p => p.UserLogin == login);

                if (us.Count() > 0)
                {
                    Users exUser        = us.First();
                    bool  alreadyActive = exUser.UserPsw.Length > 0;

                    if (alreadyActive)
                    {
                        return(false);
                    }
                    // Восстановление удаленного
                    exUser.UserPsw = newUser.UserPsw;
                    newUser        = exUser;
                }
                else
                {
                    db.AddToUsers(newUser);
                }
                db.SaveChanges();

                int       newId       = newUser.ID;
                UserRoles newUserRole = new UserRoles()
                {
                    RoleId = roleId,
                    UserId = newId
                };
                db.AddToUserRoles(newUserRole);
                db.SaveChanges();
            }
            return(true);
        }
Esempio n. 5
0
        public void SaveContract(string title)
        {
            int userId = Ext.GetUserId();

            using (RepositoryEntities db = new RepositoryEntities())
            {
                Contracts newContract = new Contracts()
                {
                    Title  = title,
                    UserId = userId,
                    Status = false
                };
                db.AddToContracts(newContract);
                db.SaveChanges();
            }
        }
Esempio n. 6
0
        public void UpdateUser(int id, string psw, string role)
        {
            using (RepositoryEntities db = new RepositoryEntities())
            {
                int    roleId  = db.Roles.Where(p => p.Name == role).Select(p => p.Id).First();
                string userPsw = psw == null ? psw : GetHash(psw);

                db.UserRoles.Where(p => p.UserId == id).ForEach(x => x.RoleId = roleId);

                if (psw != null)
                {
                    db.Users.Where(p => p.ID == id).ForEach(x => x.UserPsw = userPsw);
                }
                db.SaveChanges();
            }
        }