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); }
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); }
public void Delete(int id) { using (RepositoryEntities db = new RepositoryEntities()) { db.DeleteObject(db.Contracts.Where(p => p.ID == id).First()); db.SaveChanges(); } }
public void Update(int id) { using (RepositoryEntities db = new RepositoryEntities()) { db.Contracts.Where(p => p.ID == id).ForEach(x => x.Status = true); db.SaveChanges(); } }
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); } }
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()); } }
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(); } }
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(); } }
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); } } }
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); }
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); } }
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); }
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); } } }
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[] { });; } } }