Esempio n. 1
0
    public async Task AutoproxyAccount(Context ctx)
    {
        if (!ctx.HasNext())
        {
            await ctx.Reply($"Autoproxy is currently **{EnabledDisabled(ctx.MessageContext.AllowAutoproxy)}** for account <@{ctx.Author.Id}>.");

            return;
        }

        var allow = ctx.MatchToggle(true);

        var statusString = EnabledDisabled(allow);

        if (ctx.MessageContext.AllowAutoproxy == allow)
        {
            await ctx.Reply($"{Emojis.Note} Autoproxy is already {statusString} for account <@{ctx.Author.Id}>.");

            return;
        }
        var patch = new AccountPatch {
            AllowAutoproxy = allow
        };
        await ctx.Repository.UpdateAccount(ctx.Author.Id, patch);

        await ctx.Reply($"{Emojis.Success} Autoproxy {statusString} for account <@{ctx.Author.Id}>.");
    }
Esempio n. 2
0
        public async Task <Account> UpdateAccountAsync(
            string requestId,
            string subscriptionId,
            string resourceGroupName,
            string accountName,
            AccountPatch accountPatch)
        {
            await this.ValidateSubscriptionRegistration(subscriptionId);

            var paramTenant = new Tenant
            {
                SubscriptionId    = subscriptionId,
                ResourceGroupName = resourceGroupName,
                AccountName       = accountName,
                Tags  = accountPatch.Tags ?? new Dictionary <string, string>(),
                State = TenantState.Active
            };

            var tenant = await this.tenantCacheClient.UpdateTenantAsync(
                requestId,
                paramTenant);

            return(await Task.FromResult(new Account
            {
                Id = ResourceIdHelper.GetAccountId(
                    tenant.SubscriptionId,
                    tenant.ResourceGroupName,
                    tenant.AccountName),
                Name = tenant.AccountName,
                Type = NameStore.FullyQualifiedAccountResourceType,
                Location = tenant.Location,
                SKU = SkuStore.GetSKU(tenant.SKU),
                Tags = tenant.Tags
            }));
        }
Esempio n. 3
0
    public async Task UpdateAccount(ulong id, AccountPatch patch)
    {
        _logger.Information("Updated account {accountId}: {@AccountPatch}", id, patch);
        var query = patch.Apply(new Query("accounts").Where("uid", id));

        _ = _dispatch.Dispatch(id, patch);
        await _db.ExecuteQuery(query, "returning *");
    }
Esempio n. 4
0
        /// <summary>
        /// Creates an account.
        /// </summary>
        /// <param name="query"></param>
        /// <param name="payloadObject"></param>
        /// <returns></returns>
        public override RestResponse OnPost(Query query, object payloadObject)
        {
#if !SKIP_SSL_ENFORCE
            //Server must be in SSL mode
            if (!Handler.ApiHandler.IsSecure)
            {
                return(new RestResponse(RestStatus.Forbidden, msg: "Cannot set passwords if the server is not SSL"));
            }
#endif

            //Validate the account
            AccountPatch account = (AccountPatch)payloadObject;
            if (string.IsNullOrWhiteSpace(account.Name))
            {
                return(new RestResponse(RestStatus.BadRequest, msg: "Account name cannot be empty"));
            }
            if (string.IsNullOrWhiteSpace(account.Password))
            {
                return(new RestResponse(RestStatus.BadRequest, msg: "Password cannot be empty"));
            }

            //Only SuperBot and SuperUsers can create admin
            if (account.IsAdmin.GetValueOrDefault(false) && AuthenticationLevel < AuthLevel.SuperBot)
            {
                return(new RestResponse(RestStatus.Forbidden, "Only SuperBot or above may create admin accounts."));
            }

            var task = Task.Run(async() =>
            {
                //Make sure the name isnt a duplicate
                if (await Starbound.Configurator.GetAccountAsync(account.Name) != null)
                {
                    return(new RestResponse(RestStatus.BadRequest, $"The username {account.Name} already exists."));
                }

                //Post it
                await Starbound.Configurator.SetAccountAsync(account.ToAccount());

                //Save the settings
                await Starbound.SaveConfigurationAsync(true);
                return(new RestResponse(RestStatus.OK, msg: $"Saved: " + account.Name, res: account));
            });

            if (query.GetBool(Query.AsyncKey, false))
            {
                return(RestResponse.Async);
            }
            return(task.Result);
        }
Esempio n. 5
0
        private async Task AutoproxyEnableDisable(Context ctx, bool allow)
        {
            var statusString = allow ? "enabled" : "disabled";

            if (ctx.MessageContext.AllowAutoproxy == allow)
            {
                await ctx.Reply($"{Emojis.Note} Autoproxy is already {statusString} for account <@{ctx.Author.Id}>.", mentions : new IMention[] {});

                return;
            }
            var patch = new AccountPatch {
                AllowAutoproxy = allow
            };
            await _db.Execute(conn => _repo.UpdateAccount(conn, ctx.Author.Id, patch));

            await ctx.Reply($"{Emojis.Success} Autoproxy {statusString} for account <@{ctx.Author.Id}>.", mentions : new IMention[] {});
        }
Esempio n. 6
0
 public async Task <Account> UpdateAccountAsync(
     string requestId,
     string subscriptionId,
     string resourceGroupName,
     string accountName,
     AccountPatch accountPatch)
 {
     return(await Task.FromResult(new Account
     {
         Id = ResourceIdHelper.GetAccountId(subscriptionId, resourceGroupName, accountName),
         Name = accountName,
         Type = NameStore.FullyQualifiedAccountResourceType,
         Location = "China North",
         SKU = SkuStore.B1,
         Tags = accountPatch.Tags
     }));
 }
        public async Task <Account> UpdateAsync(
            [GlobalParameter("subscriptionId")] string subscriptionId,
            [GlobalParameter("resourceGroupName")] string resourceGroupName,
            [GlobalParameter("accountName")] string accountName,
            [FromBody] AccountPatch accountPatch,
            [GlobalParameter("api-version"), FromQuery("api-version")] string apiVersion)
        {
            ApiVersionStore.ValidateApiVersion(apiVersion);
            Validator.ArgumentValidGuid(subscriptionId, nameof(subscriptionId));
            Validator.ArgumentNotNullOrrWhiteSpace(resourceGroupName, nameof(resourceGroupName));
            Validator.ArgumentNotNullOrrWhiteSpace(accountName, nameof(accountName));

            this.LogActionBegin($"Tags = {this.SerializeTags(accountPatch.Tags)}");

            var result = await this.accountManager.UpdateAccountAsync(
                this.Request.GetRequestId(),
                subscriptionId,
                resourceGroupName,
                accountName,
                accountPatch);

            this.LogActionEnd($"Tags = {this.SerializeTags(result.Tags)}");
            return(result);
        }
Esempio n. 8
0
        /// <summary>
        /// Updates the account
        /// </summary>
        /// <param name="query"></param>
        /// <param name="payloadObject"></param>
        /// <returns></returns>
        public override RestResponse OnPatch(Query query, object payloadObject)
        {
            AccountPatch oa = (AccountPatch)payloadObject;

            if (oa.IsAdmin.HasValue && AuthenticationLevel < AuthLevel.SuperBot)
            {
                return(new RestResponse(RestStatus.Forbidden, msg: "Only SuperBots or above may patch admin accounts"));
            }

#if !SKIP_SSL_ENFORCE
            if (oa.Password != null && !Handler.ApiHandler.IsSecure)
            {
                return(new RestResponse(RestStatus.Forbidden, msg: "Cannot set passwords if the server is not SSL"));
            }
#endif

            string username = oa.Name ?? Account.Name;
            string password = oa.Password ?? Account.Password;
            bool   isAdmin  = oa.IsAdmin ?? Account.IsAdmin;
            bool   isActive = oa.IsActive ?? Account.IsActive;

            //Prepare the task
            var task = Task.Run(async() =>
            {
                if (username != Account.Name)
                {
                    //Make sure the name isnt a duplicate
                    if (Starbound.Configurator.GetAccountAsync(username) != null)
                    {
                        return(new RestResponse(RestStatus.BadRequest, "The username " + username + " already exists."));
                    }

                    //Remove the old account
                    await Starbound.Configurator.RemoveAccountAsync(Account);

                    //Create the new account
                    Account = new Account(username)
                    {
                        IsAdmin  = isAdmin,
                        Password = password,
                        IsActive = isActive
                    };
                }
                else
                {
                    //Edit the individual parts of the account
                    Account.Password = password;
                    Account.IsAdmin  = isAdmin;
                    Account.IsActive = isActive;
                }

                //Set the account
                await Starbound.Configurator.SetAccountAsync(Account);

                //Terminate any connections
                var auth = this.Handler.ApiHandler.GetAuthentication(Account.Name);
                this.Handler.ApiHandler.DisconnectAuthentication(auth, reason: "Authentication change");

                //Logout anyone that previously connected
                foreach (var player in Starbound?.Connections?.GetCopiedPlayersEnumerable().Where(p => p != null && p.AccountName != null && p.AccountName.Equals(Account.Name)))
                {
                    await player.Kick("Account details changed.");
                }

                //Apply the settings
                await Starbound.SaveConfigurationAsync(true);
                return(OnGet(query));
            });

            //If we are async, abort asap, otherwise wait for it to finish.
            if (query.GetBool(Query.AsyncKey, false))
            {
                return(RestResponse.Async);
            }
            return(task.Result);
        }