Ejemplo n.º 1
0
        public bool IsAuthorized(IApiResource fromResource, Endpoint targetEndpoint)
        {
            var gatekeeperAttr = targetEndpoint.Action.GetAttribute <SafeEndpointAttribute>();

            if (gatekeeperAttr != null)
            {
                if (gatekeeperAttr.LevelRequired == PermissionLevel.None)
                {
                    return(true);
                }

                var query =
                    Context
                    .Permissions
                    .Where(x =>
                           x.ResourceCode == targetEndpoint.ResourceCode);

                query =
                    query
                    .Where(x =>
                           x.LevelOnAny >= gatekeeperAttr.LevelRequired ||
                           (x.LevelOnOwned >= gatekeeperAttr.LevelRequired &&
                            Context.User.OkAuthCredentialId == fromResource.OwnerId));


                bool hasPermission = query.Any();
                return(hasPermission);
            }

            return(false);
        }
Ejemplo n.º 2
0
        protected Dictionary <string, string> GetElementLinks(IApiResource resource)
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            var linkMap           = new Dictionary <string, string>();
            var relevantEndpoints = HateoasService.GetElementLinks(resource, Authorizator);

            foreach (Endpoint endpoint in relevantEndpoints)
            {
                string actionName = endpoint.EndpointName;

                if (endpoint.IsElementGetter)
                {
                    actionName = "self";
                }

                string href = "/" + endpoint.UrlTemplate;

                href = ReflectionUtils.ReplaceUrlTemplate(href, resource);

                linkMap.Add(actionName, href);
            }
            return(linkMap);
        }
Ejemplo n.º 3
0
        public static string SelectFormattedValue(string propertyName, IApiResource apiResource, bool isRawValue = false, bool isWithUnit = false, bool useDefaultUnit = false)
        {
            string rawValue = apiResource.GetValue(propertyName);

            var description = apiResource.GetDescription(propertyName);

            if (isRawValue)
            {
                return(isWithUnit ? rawValue + description.Unit : rawValue);
            }
            else
            {
                if (useDefaultUnit)
                {
                    // 定義済みの「円」単位の数値は 100万円に変換する
                    if (DefaultUnitConfig.MillionYenProperties.Contains(description.Name) && description.Unit.Equals("円"))
                    {
                        description = new PropertyDescription(description.Name, description.JpName, "百万円");
                    }
                }

                var    formatter      = PropertyFormatterFactory.Create(description);
                string formattedValue = formatter.Format(rawValue, description);

                return(isWithUnit ? formattedValue + description.Unit : formattedValue);
            }
        }
Ejemplo n.º 4
0
        protected ObjectResult Element <TElement>(IApiResource elementToReturn)
            where TElement : IApiResource
        {
            var view = new ElementView <TElement>(elementToReturn);

            return(new ObjectResult(view));
        }
Ejemplo n.º 5
0
        protected ResourceResponseBase(HttpStatusCode httpStatusCode, IApiResource resource) : base(httpStatusCode)
        {
            var location = new ResourceLocation();

            resource.SetLocation(location);
            Headers.Location = location.Location;
        }
Ejemplo n.º 6
0
 protected ResourceResponseBase(HttpStatusCode httpStatusCode, IApiResource resource)
     : base(httpStatusCode)
 {
     var location = new ResourceLocation();
     resource.SetLocation(location);
     Headers.Location = location.Location;
 }
Ejemplo n.º 7
0
        private IEnumerable <Endpoint> GetEndpoints(IApiResource resource, EndpointScope scope, EndpointType type, IHateoasAuthorizator authorizator)
        {
            var links =
                Endpoints
                .Where(x => x.Type == type &&
                       x.Scope == scope &&
                       x.ResourceCode == resource.ApiResourceCode &&
                       authorizator.IsAuthorized(resource, x));

            return(links);
        }
Ejemplo n.º 8
0
        public HateoasElementView GetHateoasElementView(IApiResource resource)
        {
            string             parentResourceCode = CurrentEndpoint.ResourceCode;
            HateoasLabeledLink parentLink         = this.GetParentLink(parentResourceCode, resource.ApiResourceCode);

            var elemLinks   = GetElementLinks(resource);
            var elemActions = GetElementActions(resource);

            var view = new HateoasElementView(elemLinks, elemActions, resource, parentLink);

            return(view);
        }
Ejemplo n.º 9
0
        protected Dictionary <string, HateoasAction> GetElementActions(IApiResource resource)
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            var hateoasActionMap  = new Dictionary <string, HateoasAction>();
            var relevantEndpoints = HateoasService.GetElementActions(resource, Authorizator);

            foreach (Endpoint endpoint in relevantEndpoints)
            {
                HateoasAction newAction = new HateoasAction();
                newAction.Method = endpoint.HttpMethod;
                newAction.Href   = "/" + endpoint.UrlTemplate;

                newAction.Href = ReflectionUtils.ReplaceUrlTemplate(newAction.Href, resource);

                hateoasActionMap.Add(endpoint.EndpointName, newAction);
            }

            return(hateoasActionMap);
        }
Ejemplo n.º 10
0
 public NotModifiedResponse(IApiResource resource)
     : base(HttpStatusCode.NotModified, resource)
 {
 }
Ejemplo n.º 11
0
 public ApiService(IApiResource apiResource)
 {
     _apiResource = apiResource;
 }
Ejemplo n.º 12
0
 public ConflictResponse(IApiResource apiResource)
     : base(HttpStatusCode.Conflict, apiResource)
 {
 }
Ejemplo n.º 13
0
 public ConflictResponse(IApiResource apiResource)
     : base(HttpStatusCode.Conflict, apiResource)
 {
 }
Ejemplo n.º 14
0
 public CreateResponse(IApiResource resource)
     : base(HttpStatusCode.Created, resource)
 {
 }
 public FormattedValueSelector(IApiResource apiResource)
 {
     this.apiResource = apiResource;
 }
Ejemplo n.º 16
0
 public ElementView(IApiResource viewModel)
 {
     this.ViewModel = viewModel;
 }
Ejemplo n.º 17
0
 public NotModifiedResponse(IApiResource resource)
     : base(HttpStatusCode.NotModified, resource)
 {
 }
Ejemplo n.º 18
0
 public CreateResponse(IApiResource resource)
     : base(HttpStatusCode.Created, resource)
 {
 }
Ejemplo n.º 19
0
 public bool IsAuthorized(IApiResource fromResource, Endpoint targetEndpoint)
 {
     return(true);
 }
Ejemplo n.º 20
0
        public IEnumerable <Endpoint> GetElementLinks(IApiResource resource, IHateoasAuthorizator authorizator)
        {
            var links = this.GetEndpoints(resource, EndpointScope.Element, EndpointType.Link, authorizator);

            return(links);
        }
Ejemplo n.º 21
0
        public IEnumerable <Endpoint> GetElementActions(IApiResource resource, IHateoasAuthorizator authorizator)
        {
            var actions = this.GetEndpoints(resource, EndpointScope.Element, EndpointType.Action, authorizator);

            return(actions);
        }