public static void AddUserToRole(Guid UserId, string roleName) { using (CarrotCMSDataContext _db = CarrotCMSDataContext.Create()) { membership_Role role = (from r in _db.membership_Roles where r.Name.ToLower() == roleName.ToLower() select r).FirstOrDefault(); membership_User user = (from u in _db.membership_Users join ud in _db.carrot_UserDatas on u.Id equals ud.UserKey where ud.UserId == UserId select u).FirstOrDefault(); membership_UserRole usrRole = (from r in _db.membership_Roles join ur in _db.membership_UserRoles on r.Id equals ur.RoleId join u in _db.membership_Users on ur.UserId equals u.Id join ud in _db.carrot_UserDatas on u.Id equals ud.UserKey where r.Name.ToLower() == roleName.ToLower() && ud.UserId == UserId select ur).FirstOrDefault(); if (usrRole == null && role != null && user != null) { usrRole = new membership_UserRole(); usrRole.UserId = user.Id; usrRole.RoleId = role.Id; _db.membership_UserRoles.InsertOnSubmit(usrRole); _db.SubmitChanges(); } } }
public static bool AddUserToRole(string userName, string roleName) { using (CarrotCMSDataContext _db = CarrotCMSDataContext.Create()) { membership_Role role = (from r in _db.membership_Roles where r.Name == roleName select r).FirstOrDefault(); membership_User user = (from u in _db.membership_Users where u.UserName == userName select u).FirstOrDefault(); membership_UserRole usrRole = (from r in _db.membership_Roles join ur in _db.membership_UserRoles on r.Id equals ur.RoleId join u in _db.membership_Users on ur.UserId equals u.Id where r.Name == roleName && u.UserName == userName select ur).FirstOrDefault(); if (usrRole == null && role != null && user != null) { usrRole = new membership_UserRole(); usrRole.UserId = user.Id; usrRole.RoleId = role.Id; _db.membership_UserRoles.InsertOnSubmit(usrRole); _db.SubmitChanges(); return(true); } return(false); } }
public static void RemoveUserFromRole(string userName, string roleName) { using (CarrotCMSDataContext _db = CarrotCMSDataContext.Create()) { membership_UserRole usrRole = (from r in _db.membership_Roles join ur in _db.membership_UserRoles on r.Id equals ur.RoleId join u in _db.membership_Users on ur.UserId equals u.Id where r.Name.ToLower() == roleName.ToLower() && u.UserName.ToLower() == userName.ToLower() select ur).FirstOrDefault(); if (usrRole != null) { _db.membership_UserRoles.DeleteOnSubmit(usrRole); _db.SubmitChanges(); } } }
public static void AddUserToRole(Guid UserId, string roleName) { using (CarrotCMSDataContext _db = CarrotCMSDataContext.Create()) { membership_Role role = (from r in _db.membership_Roles where r.Name.ToLower() == roleName.ToLower() select r).FirstOrDefault(); membership_User user = (from u in _db.membership_Users join ud in _db.carrot_UserDatas on u.Id equals ud.UserKey where ud.UserId == UserId select u).FirstOrDefault(); membership_UserRole usrRole = (from r in _db.membership_Roles join ur in _db.membership_UserRoles on r.Id equals ur.RoleId join u in _db.membership_Users on ur.UserId equals u.Id join ud in _db.carrot_UserDatas on u.Id equals ud.UserKey where r.Name.ToLower() == roleName.ToLower() && ud.UserId == UserId select ur).FirstOrDefault(); if (usrRole == null && role != null && user != null) { usrRole = new membership_UserRole(); usrRole.UserId = user.Id; usrRole.RoleId = role.Id; _db.membership_UserRoles.InsertOnSubmit(usrRole); _db.SubmitChanges(); } } }
public static bool AddUserToRole(string userName, string roleName) { using (CarrotCMSDataContext _db = CarrotCMSDataContext.Create()) { membership_Role role = (from r in _db.membership_Roles where r.Name == roleName select r).FirstOrDefault(); membership_User user = (from u in _db.membership_Users where u.UserName == userName select u).FirstOrDefault(); membership_UserRole usrRole = (from r in _db.membership_Roles join ur in _db.membership_UserRoles on r.Id equals ur.RoleId join u in _db.membership_Users on ur.UserId equals u.Id where r.Name == roleName && u.UserName == userName select ur).FirstOrDefault(); if (usrRole == null && role != null && user != null) { usrRole = new membership_UserRole(); usrRole.UserId = user.Id; usrRole.RoleId = role.Id; _db.membership_UserRoles.InsertOnSubmit(usrRole); _db.SubmitChanges(); return true; } return false; } }