예제 #1
0
        public bool ChangeBaseUserRight(out bool _bInternalErrorOccured, bool _bDoNotGetDBClearance, EChangeUserRightsForModelType _Type, string _UserID, string _PathRegex, Action <string> _ErrorMessageAction, List <string> _Rights = null)
        {
            _bInternalErrorOccured = false;

            Task <HttpResponseMessage> RequestTask = null;
            StringContent RequestContent           = null;

            using var Handler = new HttpClientHandler
                  {
                      SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls,
                      ServerCertificateCustomValidationCallback = (a, b, c, d) => true
                  };
            using var Client = new HttpClient(Handler);
            Client.DefaultRequestHeaders.TryAddWithoutValidation("internal-call-secret", CommonData.INTERNAL_CALL_PRIVATE_KEY);
            Client.DefaultRequestHeaders.TryAddWithoutValidation("do-not-get-db-clearance", _bDoNotGetDBClearance ? "true" : "false");
            try
            {
                if (_Type == EChangeUserRightsForModelType.Add)
                {
                    RequestContent = new StringContent(
                        new JArray()
                    {
                        JObject.Parse(JsonConvert.SerializeObject(
                                          new AccessScope()
                        {
                            WildcardPath = _PathRegex,
                            AccessRights = _Rights
                        }))
                    }.ToString(), Encoding.UTF8, "application/json");

                    RequestTask = Client.PutAsync("http://localhost:" + LocalServerPort + "/auth/users/" + _UserID + "/base_access_rights", RequestContent);
                }
                else
                {
                    RequestTask = Client.DeleteAsync("http://localhost:" + LocalServerPort + "/auth/users/" + _UserID + "/base_access_rights/" + WebUtility.UrlEncode(_PathRegex));
                }

                RequestTask.Wait();

                using var Response        = RequestTask.Result;
                using var ResponseContent = Response.Content;

                using var ReadResponseTask = ResponseContent.ReadAsStringAsync();
                ReadResponseTask.Wait();

                var ResponseString = ReadResponseTask.Result;

                if (!Response.IsSuccessStatusCode)
                {
                    _bInternalErrorOccured = (int)Response.StatusCode == BWebResponse.Error_InternalError_Code;
                    _ErrorMessageAction?.Invoke("Error: Controller_Rights_Internal->ChangeBaseUserRight: Request returned error. Code: " + Response.StatusCode + ", message: " + ResponseString);
                    return(false);
                }
            }
            catch (Exception e)
            {
                if (e.InnerException != null && e.InnerException != e)
                {
                    _ErrorMessageAction?.Invoke("Error: Controller_Rights_Internal->ChangeBaseUserRight->Inner: " + e.InnerException.Message + ", Trace: " + e.InnerException.StackTrace);
                }
                if (e is AggregateException)
                {
                    foreach (var Inner in (e as AggregateException).InnerExceptions)
                    {
                        _ErrorMessageAction?.Invoke("Error: Controller_Rights_Internal->ChangeBaseUserRight->Aggregate->Inner: " + Inner.Message + ", Trace: " + Inner.StackTrace);
                    }
                }
                _bInternalErrorOccured = true;
                _ErrorMessageAction?.Invoke("Error: Controller_Rights_Internal->ChangeBaseUserRight: Request failed. Message: " + e.Message + ", trace: " + e.StackTrace);
                return(false);
            }
            finally
            {
                try { RequestContent?.Dispose(); } catch (Exception) { }
                try { RequestTask?.Dispose(); } catch (Exception) { }
            }
            return(true);
        }