protected void UpdateUser(UserPrincipal up, UserInfo user) { user.Name = up.DisplayName; user.Email = up.EmailAddress; user.Active = true; foreach (var g in up.GetGroups()) { user.MemberOf.Add(g.SamAccountName); } user.ExtId = up.DistinguishedName; }
public bool PasswordAuthenticate(string user, string password) { var usr = Db.GetCollection<UserInfo>().Find(Query.EQ("Login", user)).FirstOrDefault(); if (usr == null) { log.Info("user not found: {0}", user); return false; usr = new UserInfo { Login = user, Passwd = password, Email = user + "@wypas", Active = true }; Db.GetCollection<UserInfo>().Insert(usr); return true; } log.Info("User {0} found: {1}", usr.Login, usr.Id); return true; }
protected UserInfo UpdateUserInfo(WindowsIdentity wi, UserInfo ui) { ui.Login = wi.Name; ui.MemberOf = new List<string>(); ui.MemberOf.Add("ALL"); ui.Name = wi.User.Translate(typeof(NTAccount)).Value; string[] pts = wi.Name.Split('\\'); string domain = pts.Length > 1 ? pts[0] : null; foreach (var g in wi.Groups) { var gn = g.Translate(typeof(NTAccount)).Value; if (gn == null) continue; if (domain != null) { if (!gn.StartsWith(domain + '\\')) continue; gn = gn.Substring(domain.Length + 1); } if (!ui.MemberOf.Contains(g.Value)) ui.MemberOf.Add(g.Value); } return ui; }
protected virtual UserInfo UserFound(IIdentity identity, UserInfo userRec) { return userRec; }
public void SaveUser(UserInfo ui) { if (string.IsNullOrEmpty(ui.Id)) { Db.GetCollection<UserInfo>().Save(ui); } else { Db.GetCollection<UserInfo>().Update(Query.EQ("_id", ui.Id), Update.Set("Name", ui.Name).Set("Email", ui.Email).Set("Login", ui.Login) .Set("NeedsSync", ui.NeedsSync).Set("Active", ui.Active) .Set("MemberOf", MongoDB.Bson.BsonArray.Create(ui.MemberOf)).Set("ExtId", ui.ExtId)); } }
protected override UserInfo UserNotFound(IIdentity identity) { if (!(identity.IsAuthenticated)) throw new Exception("Identity not authenticated"); log.Info("Creating new user record for {0}", identity.Name); int idx = identity.Name.IndexOf('\\'); string login = idx < 0 ? identity.Name : identity.Name.Substring(idx + 1); log.Info("Searching for login {0}", login); using (var pc = GetPrincipalContext()) { UserPrincipal up = UserPrincipal.FindByIdentity(pc, login); if (up == null) { log.Info("User not found: {0}", login); return null; } log.Info("Creating new user record for {0}", up.DistinguishedName); var ui = new UserInfo(); ui.Login = identity.Name; ui.Active = true; ui.MemberOf.Add("ALL"); UpdateUser(up, ui); Db.GetCollection<UserInfo>().Save(ui); log.Info("User {0} saved: {1}", ui.Login, ui.Id); return ui; } }
protected override UserInfo UserFound(IIdentity identity, UserInfo userRec) { return base.UserFound(identity, userRec); }