package aaa import ( ice "github.com/shylinux/icebergs" "github.com/shylinux/icebergs/base/cli" "github.com/shylinux/icebergs/base/gdb" "github.com/shylinux/icebergs/base/mdb" kit "github.com/shylinux/toolkits" "strings" ) func _user_list(m *ice.Message) { m.Richs(USER, nil, kit.MDB_FOREACH, func(key string, value map[string]interface{}) { m.Push(key, value, []string{kit.MDB_TIME, USERZONE, USERNICK, USERNAME}) }) m.Sort(USERNAME) } func _user_login(m *ice.Message, name, word string) (ok bool) { m.Debug("what %v %v", name, word) if m.Richs(USER, nil, name, nil) == nil { _user_create(m, name, "") } m.Richs(USER, nil, name, func(key string, value map[string]interface{}) { m.Debug("what %v %v", name, word) if kit.Format(value[PASSWORD]) == "" { ok, value[PASSWORD] = true, word } else if value[PASSWORD] == word { ok = true } }) return ok } func _user_modify(m *ice.Message, name string, arg ...string) { if m.Richs(USER, nil, name, nil) == nil { _user_create(m, name, "") } m.Log_MODIFY(USERNAME, name, arg) m.Richs(USER, nil, name, func(key string, value map[string]interface{}) { for i := 0; i < len(arg)-1; i += 2 { kit.Value(value, arg[i], arg[i+1]) } }) } func _user_create(m *ice.Message, name, word string) { h := m.Rich(USER, nil, kit.Dict( USERNAME, name, PASSWORD, word, USERNICK, name, USERNODE, cli.NodeName, USERZONE, m.Option(ice.MSG_USERZONE), USERNODE, cli.NodeName, )) m.Log_CREATE(USERNAME, name, "hash", h) m.Event(gdb.USER_CREATE, name) } func _user_search(m *ice.Message, kind, name, text string, arg ...string) { m.Richs(USER, nil, kit.MDB_FOREACH, func(key string, val map[string]interface{}) { if name != "" && name != val[USERNAME] { return } m.Push("pod", m.Option("pod")) m.Push("ctx", "aaa") m.Push("cmd", USER) m.Push(key, val, []string{kit.MDB_TIME}) m.Push(kit.MDB_SIZE, kit.Format("")) m.Push(kit.MDB_TYPE, kit.Format(UserRole(m, val[USERNAME]))) m.Push(kit.MDB_NAME, kit.Format(val[USERNICK])) m.Push(kit.MDB_TEXT, kit.Format(val[USERNAME])) }) } func UserRoot(m *ice.Message) { cli.PassWord = kit.Hashs("uniq") cli.PassWord = cli.UserName _user_create(m, cli.UserName, cli.PassWord) } func UserNick(m *ice.Message, username interface{}) (nick string) { m.Richs(USER, nil, kit.Format(username), func(key string, value map[string]interface{}) { nick = kit.Format(value[USERNICK]) }) return } func UserZone(m *ice.Message, username interface{}) (zone string) { m.Richs(USER, nil, kit.Format(username), func(key string, value map[string]interface{}) { zone = kit.Format(value[USERZONE]) }) return } func UserRole(m *ice.Message, username interface{}) (role string) { if role = VOID; username == cli.UserName { return ROOT } m.Richs(ROLE, nil, TECH, func(key string, value map[string]interface{}) { if kit.Value(value, kit.Keys(USER, username)) == true { role = TECH } }) return } func UserLogin(m *ice.Message, username, password string) bool { if _user_login(m, username, password) { m.Option(ice.MSG_USERNAME, username) m.Option(ice.MSG_USERROLE, UserRole(m, username)) m.Log_AUTH(USERROLE, m.Option(ice.MSG_USERROLE), USERNAME, m.Option(ice.MSG_USERNAME), PASSWORD, strings.Repeat("*", len(password))) return true } return false } const USER = "user" func init() { Index.Merge(&ice.Context{ Configs: map[string]*ice.Config{ USER: {Name: "user", Help: "用户", Value: kit.Data(kit.MDB_SHORT, USERNAME)}, }, Commands: map[string]*ice.Command{ USER: {Name: "user", Help: "用户", Action: map[string]*ice.Action{ mdb.CREATE: {Name: "create username [password]", Help: "创建", Hand: func(m *ice.Message, arg ...string) { _user_create(m, arg[0], kit.Select("", arg, 1)) }}, mdb.MODIFY: {Name: "modify [key value]...", Help: "编辑", Hand: func(m *ice.Message, arg ...string) { _user_modify(m, m.Option(USERNAME), arg...) }}, mdb.SEARCH: {Name: "search type name text arg...", Help: "搜索", Hand: func(m *ice.Message, arg ...string) { _user_search(m, arg[0], arg[1], kit.Select("", arg, 2)) }}, "login": {Name: "login username password", Help: "登录", Hand: func(m *ice.Message, arg ...string) { _user_login(m, arg[0], arg[1]) }}, }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { _user_list(m) }}, }, }, nil) }