Ejemplo n.º 1
0
        public VirtAccount CreateOrUpdateVirtAccount([NotNull] string username, string password = null,
                                                     string group = null)
        {
            if (username == null)
            {
                throw new ArgumentNullException("username");
            }

            try
            {
                _server.LockServerSettings(SettingsLockTimeout);
                _server.LoadServerSettings();

                // check for existing user
                var query       = string.Format(@"virtAccount eqi ""{0}""", username);
                var exists      = int.Parse(_server.Query("access.virtAccounts.Count({0})", query)) > 0;
                var virtAccount = exists
                                      ? GetVirtAccounts(query).SingleOrDefault()
                                      : new VirtAccount("access.virtAccounts.New", "", "");

                if (!exists)
                {
                    _server.Command(@"access.virtAccounts.NewClear");
                }

                _server.Command(@"{0}.virtAccount ""{1}""", virtAccount.Id, username);

                if (!string.IsNullOrWhiteSpace(password))
                {
                    _server.Command(@"{0}.virtPassword.Set ""{1}""", virtAccount.Id, password);
                }

                if (!string.IsNullOrWhiteSpace(group))
                {
                    _server.Command(@"{0}.group ""{1}""", virtAccount.Id, group);
                }

                if (!exists)
                {
                    _server.Command("access.virtAccounts.NewCommit");
                }

                _server.SaveServerSettings();

                virtAccount = GetVirtAccounts(query).Single();
                return(virtAccount);
            }
            catch (COMException ex)
            {
                throw new BitviseSSHServerException(ex);
            }
            finally
            {
                _server.UnlockServerSettings();
            }
        }