/// <summary> /// Adds a user to this database. /// </summary> /// <param name="user">The user.</param> public static void AddUserEx(MongoCollection Col, User user) { var document = Col.FindOneAs<BsonDocument>(Query.EQ("user", user.Username)); if (document == null) { document = new BsonDocument("user", user.Username); } document["roles"] = user.roles; if (document.Contains("readOnly")) { document.Remove("readOnly"); } //必须要Hash一下Password document["pwd"] = MongoUser.HashPassword(user.Username, user.Password); //OtherRole 必须放在Admin.system.users里面 if (Col.Database.Name == MongoDbHelper.DATABASE_NAME_ADMIN) { document["otherDBRoles"] = user.otherDBRoles; } if (string.IsNullOrEmpty(user.Password)) { document["userSource"] = user.userSource; } Col.Save(document); }
/// <summary> /// AddUserToSystem /// </summary> /// <param name="newUserEx">用户信息</param> /// <param name="IsAdmin">是否是Admin</param> public static void AddUserToSystem(User newUserEx, Boolean IsAdmin) { MongoServer mongoSvr = SystemManager.GetCurrentServer(); //必须使用MongoCredentials来添加用户,不然的话,Password将使用明文登入到数据库中! //这样的话,在使用MongoCredentials登入的时候,会发生密码错误引发的认证失败 MongoCollection users; users = IsAdmin ? mongoSvr.GetDatabase(MongoDbHelper.DATABASE_NAME_ADMIN).GetCollection(MongoDbHelper.COLLECTION_NAME_USER) : SystemManager.GetCurrentDataBase().GetCollection(MongoDbHelper.COLLECTION_NAME_USER); //以下代码 1.Ver2.4以前的有ReadOnly,FindUser需要寻找ReadOnly字段 // 2.这个其实不用检查,有的话修改,没有的话,新建 //if (users.Database.FindUser(newUserEx.Username) == null) //{ AddUserEx(users, newUserEx); //} }
/// <summary> /// 确定 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cmdOK_Click(object sender, EventArgs e) { if (txtConfirmPsw.Text != txtPassword.Text) { MyMessageBox.ShowMessage("Error", "Password and Confirm Password not match!"); return; } //MongoUser不能同时具备Password和userSource字段! var user = new User { Username = txtUserName.Text, Password = txtUserName.Text, roles = userRoles.getRoles() }; var otherDBRoles = new BsonDocument(); foreach (BsonElement item in OtherDBRolesDict.Values) { otherDBRoles.Add(item); } user.otherDBRoles = otherDBRoles; user.userSource = txtuserSource.Text; if (txtUserName.Text == String.Empty) { MyMessageBox.ShowMessage("Error", "Please fill username!"); return; } //2013/08/13 用户结构发生大的变化 //取消了ReadOnly字段,添加了Roles等字段 //简化逻辑,不论新建还是修改,AddUser都可以 try { User.AddUserToSystem(user, _IsAdmin); } catch (Exception ex) { SystemManager.ExceptionDeal(ex); } Close(); }
/// <summary> /// </summary> /// <param name="db"></param> /// <param name="Username"></param> public void AddUser(MongoDatabase db, String Username) { var userInfo = db.GetCollection(MongoDbHelper.COLLECTION_NAME_USER).FindOneAs<BsonDocument>(Query.EQ("user", Username)); if (userInfo != null) { var user = new User(); user.roles = userInfo["roles"].AsBsonArray; if (userInfo.Contains("otherDBRoles")) { user.otherDBRoles = userInfo["otherDBRoles"].AsBsonDocument; } UserList.Add(db.Name, user); } }