Exemple #1
0
        public static IEnumerable <AccountItem> GetData()
        {
            List <AccountItem> ret = new List <AccountItem>();

            using (RepositoryEntities db = new RepositoryEntities())
            {/*
              * var ret = (from us in db.Users
              *            from rl in db.UserRoles
              *            from r in db.Roles
              *            where us.ID == rl.UserId && r.Id == rl.RoleId && us.UserPsw.Length > 0
              *            select (new AccountItem()
              *            {
              *                ID = us.ID,
              *                Login = us.UserLogin,
              *                Password = us.UserPsw,
              *                Role = r.Name
              *            })).ToList();
              *
              * ret.Sort((a,b) => { return (b.ID == a.ID) ? 0 : b.ID > a.ID ? -1 : 1; });
              * return ret;*/
                db.Users.Where(p => p.UserPsw.Length > 0).ForEach(x =>
                {
                    AccountItem it = new AccountItem()
                    {
                        ID       = x.ID,
                        Login    = x.UserLogin,
                        Password = x.UserPsw,
                        Role     = string.Join(" ", System.Web.Security.Roles.GetRolesForUser(x.UserLogin)).Trim()
                    };
                    ret.Add(it);
                });
            }
            return(ret);
        }
Exemple #2
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);
        }
Exemple #3
0
 public void Delete(int id)
 {
     using (RepositoryEntities db = new RepositoryEntities())
     {
         db.DeleteObject(db.Contracts.Where(p => p.ID == id).First());
         db.SaveChanges();
     }
 }
Exemple #4
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();
     }
 }
Exemple #5
0
 public static int GetUserId()
 {
     using (RepositoryEntities db = new RepositoryEntities())
     {
         string login  = Membership.GetUser().UserName;
         int    userId = db.Users.Where(p => p.UserLogin == login).Select(p => p.ID).First();
         return(userId);
     }
 }
Exemple #6
0
 public int GetAdminsCount()
 {
     using (RepositoryEntities db = new RepositoryEntities())
     {
         return((from ur in db.UserRoles
                 from r in db.Roles
                 where ur.RoleId == r.Id && r.Name == "Administrator"
                 select ur.UserId).Count());
     }
 }
Exemple #7
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();
            }
        }
Exemple #8
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();
            }
        }
Exemple #9
0
        public MembershipUser GetUser(string username)
        {
            using (RepositoryEntities db = new RepositoryEntities())
            {
                var result = from u in db.Users
                             where (string.Compare(u.UserLogin, username, true) == 0)
                             select u;

                if (result.Count() != 0)
                {
                    var      dbuser                   = result.FirstOrDefault();
                    string   _username                = dbuser.UserLogin;
                    int      _providerUserKey         = dbuser.ID;
                    string   _email                   = "";                // dbuser.Email;
                    string   _passwordQuestion        = "";
                    string   _comment                 = "";                // dbuser.Comments;
                    bool     _isApproved              = true;              // dbuser.IsActivated;
                    bool     _isLockedOut             = true;              // dbuser.IsLockedOut;
                    DateTime _creationDate            = DateTime.MinValue; // dbuser.CreatedDate;
                    DateTime _lastLoginDate           = DateTime.MinValue; //dbuser.LastLoginDate;
                    DateTime _lastActivityDate        = DateTime.Now;
                    DateTime _lastPasswordChangedDate = DateTime.Now;
                    DateTime _lastLockedOutDate       = DateTime.MinValue; //dbuser.LastLockedOutDate;

                    MembershipUser user = new MembershipUser("CustomMembershipProvider",
                                                             _username,
                                                             _providerUserKey,
                                                             _email,
                                                             _passwordQuestion,
                                                             _comment,
                                                             _isApproved,
                                                             _isLockedOut,
                                                             _creationDate,
                                                             _lastLoginDate,
                                                             _lastActivityDate,
                                                             _lastPasswordChangedDate,
                                                             _lastLockedOutDate);
                    return(user);
                }
                else
                {
                    return(null);
                }
            }
        }
Exemple #10
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);
        }
Exemple #11
0
        public bool Validate(string username, string password)
        {
            using (RepositoryEntities db = new RepositoryEntities())
            {
                var res = db.Users.Where(p => string.Compare(p.UserLogin, username, true) == 0);

                if (res == null || res.Count() == 0)
                {
                    return(false);
                }
                string loginString = res.First().UserPsw;
                string checkHash   = GetHash(password);

                if (loginString == null)
                {
                    return(false);
                }
                return(string.Compare(loginString, checkHash, true) == 0);
            }
        }
Exemple #12
0
        public static IEnumerable <ContractItem> GetData()
        {
            bool showAll            = !System.Web.Security.Roles.GetRolesForUser().Contains("Operator");
            List <ContractItem> ret = new List <ContractItem>();
            int userId = showAll ? -1 : Ext.GetUserId();

            using (RepositoryEntities db = new RepositoryEntities())
            {
                db.Contracts.Where(w => showAll || w.UserId == userId).ForEach(x =>
                {
                    ContractItem it = new ContractItem()
                    {
                        ID     = x.ID,
                        Title  = x.Title,
                        Status = (null == x.Status || !(bool)x.Status) ? "Составлен" : "Утвержден",
                        User   = db.Users.Where(u => u.ID == x.UserId).Select(u => u.UserLogin).First()
                    };
                    ret.Add(it);
                });
            }
            return(ret);
        }
Exemple #13
0
        public override bool IsUserInRole(string username, string roleName)
        {
            using (RepositoryEntities db = new RepositoryEntities())
            {
                Users user = db.Users.FirstOrDefault(u => u.UserLogin.Equals(username, StringComparison.CurrentCultureIgnoreCase));

                var roles = from ur in db.UserRoles.Where(p => p.UserId == user.ID)
                            from r in db.Roles
                            where ur.RoleId == r.Id
                            select r.Name;

                if (user != null)
                {
                    return(roles.Any(r =>
                                     r.Equals(roleName, StringComparison.CurrentCultureIgnoreCase)));
                }
                else
                {
                    return(false);
                }
            }
        }
Exemple #14
0
        public override string[] GetRolesForUser(string username)
        {
            using (RepositoryEntities db = new RepositoryEntities())
            {
                Users user = db.Users.FirstOrDefault(
                    u => u.UserLogin.Equals(
                        username, StringComparison.CurrentCultureIgnoreCase));

                var myRoles = from ur in db.UserRoles.Where(p => p.UserId == user.ID)
                              from r in db.Roles
                              where ur.RoleId == r.Id
                              select r.Name;

                if (myRoles != null)
                {
                    return(myRoles.ToArray());
                }
                else
                {
                    return(new string[] { });;
                }
            }
        }