Пример #1
0
            private bool ApplyUserOption(UserAccount account, string opt, string value)
            {
                switch (opt)
                {
                case "-p":
                    account.UpdatePassword(value);
                    break;

                case "-a":
                    UserAccess access;
                    if (UserAccessUtil.FromShortString(out access, value))
                    {
                        account.Access = access;
                    }
                    else
                    {
                        goto default;
                    }
                    break;

                default:
                    return(false);
                }
                return(true);
            }
Пример #2
0
 public void Load()
 {
     if (!File.Exists(FileName))
     {
         return;
     }
     lock (sync)
     {
         Clear();
         var cfg       = new IniFile(FileName, Encoding.UTF8);
         var userCount = cfg.GetSectionCount();
         users.Capacity = userCount;
         for (int i = 0; i < userCount; i++)
         {
             var sect = "user_" + i;
             // XXX: handle errors
             var        login    = cfg.GetString(sect, "login");
             var        password = cfg.GetString(sect, "password");
             UserAccess access;
             UserAccessUtil.FromShortString(out access, cfg.GetString(sect, "access"));
             var acc = new UserAccount(login, password, access);
             users.Add(login, acc);
         }
     }
 }
Пример #3
0
            public override void Run(string[] args, PipeConsole console)
            {
                if (args.Length < 4)
                {
                    PrintUsage(console);
                    return;
                }
                var    login = args[1];
                var    pass  = args[2];
                string errMsg;

                if (!NetUtil.ValidateLogin(ref login, out errMsg))
                {
                    console.WriteLine("{0}: invalid login.\r\n{1}", Root.AppName, errMsg);
                    return;
                }
                if (!NetUtil.ValidatePassword(ref pass, out errMsg))
                {
                    console.WriteLine("{0}: invalid password.\r\n{1}", Root.AppName, errMsg);
                    return;
                }
                UserAccess access;

                if (!UserAccessUtil.FromShortString(out access, args[3]))
                {
                    console.WriteLine(Root.AppName + ": invalid user access flags.");
                    console.WriteLine("valid flags:\r\n" +
                                      "    b = banned\r\n" +
                                      "    d = default\r\n" +
                                      "    a = admin\r\n" +
                                      "    ~ = max");
                    return;
                }
                var pswHash = UserAccount.TransformPassword(pass);
                var acc     = new UserAccount(login, pswHash, access);

                if (Root.Server.Users.Add(acc))
                {
                    console.WriteLine("created user " + login);
                }
                else
                {
                    console.WriteLine("user already exists");
                }
            }
Пример #4
0
 public override void Run(string[] args, PipeConsole console)
 {
     if (args.Length == 1)
     {
         console.WriteLine("user count: " + Root.Server.Users.Count);
         return;
     }
     if (args.Length >= 2)
     {
         var users = Root.Server.Users;
         var login = args[1];
         var user  = users.Find(login);
         if (user == null)
         {
             console.WriteLine("user not found");
             return;
         }
         console.WriteLine("user {0} : {1}", user.Login, UserAccessUtil.ToShortString(user.Access));
     }
 }
Пример #5
0
 public void Save()
 {
     using (var w = new StreamWriter(FileName, false, Encoding.UTF8))
     {
         int i = 0;
         lock (sync)
         {
             foreach (var kv in users)
             {
                 var acc = kv.Value;
                 w.Write("[user_");
                 w.Write(i);
                 w.WriteLine(']');
                 w.Write("login = "******"password = "******"access = ");
                 w.WriteLine(UserAccessUtil.ToShortString(acc.Access));
                 i++;
             }
         }
     }
 }