Пример #1
0
        //[Route("account")]
        public HttpResponseMessage PatchMember(PatchMember patchMember)
        {
            string      token       = Request.Headers.Authorization.Parameter;
            JwtAuthUtil jwtAuthUtil = new JwtAuthUtil();
            int         Mid         = Convert.ToInt32(jwtAuthUtil.GetId(token));


            Member member = db.Members.Find(Mid);


            if (member.Permission == "01")
            {
                patchMember.Password = Utility.GenerateHashWithSalt(patchMember.Password, member.PasswordSalt);
                patchMember.Patch(member);

                db.SaveChanges();

                var result1 = db.Members.Where(x => x.id == Mid).Select(x => new
                {
                    x.id,
                    x.name,
                    x.Tel,
                    x.MemberIntro,
                    x.PlannerSocial1,
                    x.PlannerSocial2,
                });

                //string result = JsonConvert.DeserializeObject<PatchMember>(result1).ToString();

                return(Request.CreateResponse(HttpStatusCode.OK, new { success = true, message = "成功修改", result1 }));
            }

            if (member.Permission == "02")
            {
                patchMember.Password = Utility.GenerateHashWithSalt(patchMember.Password, member.PasswordSalt);
                patchMember.Patch(member);

                db.SaveChanges();

                var result2 = db.Members.Where(x => x.id == Mid).Select(x => new
                {
                    x.id,
                    x.name,
                    x.Tel,
                    x.MemberIntro,
                    x.PlannerSocial1,
                    x.PlannerSocial2,
                    x.PlannerName,
                    x.PlannerIntro,
                    x.PlannerSocial3,
                    x.PlannerSocial4,
                });

                return(Request.CreateResponse(HttpStatusCode.OK, new { success = true, message = "規劃師成功修改", result2 }));
            }

            return(Request.CreateResponse(HttpStatusCode.NoContent, new { message = "沒東西R" }));
        }
Пример #2
0
        private IEnumerable <PatchOperationResult> ReplaceNonDynamic(object value, Operation operation, PatchMember patchMember)
        {
            var patchProperty = patchMember.JsonPatchProperty;

            if (!patchProperty.Property.Writable)
            {
                throw new Exception(); // TODO: (DG) This is int server error.
            }

            EnforceMutability(operation, patchProperty);

            var instanceValue = patchProperty.Property.ValueProvider.GetValue(patchProperty.Parent);

            // if the instance's property value is null or it is not an enumerable type
            if (instanceValue == null || !patchProperty.Property.PropertyType.IsNonStringEnumerable())
            {
                /*
                 *  Here we are going to be setting or replacing a current value:
                 *      o  If the target location is a single-value attribute, the attributes
                 *         value is replaced.
                 *      o  If the target location path specifies an attribute that does not
                 *         exist, the service provider SHALL treat the operation as an "add".
                 *         (instanceValue == null)
                 *      o  If the target location is a complex multi-valued attribute with a
                 *         value selection filter ("valuePath") and a specific sub-attribute
                 *         (e.g., "addresses[type eq "work"].streetAddress"), the matching
                 *         sub-attribute of all matching records is replaced.
                 *         (!patchProperty.Property.PropertyType.IsNonStringEnumerable())
                 */

                var conversionResultTuple = ConvertToActualType(
                    patchProperty.Property.PropertyType,
                    value,
                    instanceValue);

                if (!conversionResultTuple.CanBeConverted)
                {
                    throw new ScimPatchException(
                              ScimErrorType.InvalidValue,
                              operation);
                }

                patchProperty.Property.ValueProvider.SetValue(
                    patchProperty.Parent,
                    conversionResultTuple.ConvertedInstance);

                return(new[]
                {
                    new PatchOperationResult(
                        patchProperty,
                        instanceValue,
                        conversionResultTuple.ConvertedInstance)
                });
            }

            // Here we are going to be modifying a complex object:
            // The first case below handles the following SCIM rules:
            // o  If the target location is a multi-valued attribute and a value
            //    selection("valuePath") filter is specified that matches one or
            //    more values of the multi-valued attribute, then all matching
            //    record values SHALL be replaced.
            // o  If the target location specifies a complex attribute, a set of
            //    sub-attributes SHALL be specified in the "value" parameter, which
            //    replaces any existing values or adds where an attribute did not
            //    previously exist.  Sub-attributes that are not specified in the
            //    "value" parameter are left unchanged.
            if (patchMember.Target != null &&
                (patchMember.Target is MultiValuedAttribute ||
                 !patchMember.Target.GetType().IsTerminalObject()))
            {
                // if value is null, we're setting the MultiValuedAttribute to null
                if (value == null)
                {
                    return(new[]
                    {
                        RemoveNonDynamic(operation, patchMember)
                    });
                }

                // value should be an object composed of sub-attributes of the parent, a MultiValuedAttribute
                var operations    = new List <PatchOperationResult>();
                var resourcePatch = JObject.Parse(value.ToString());
                var jsonContract  = (JsonObjectContract)ContractResolver.ResolveContract(patchMember.Target.GetType());
                foreach (var kvp in resourcePatch)
                {
                    var attemptedProperty = jsonContract.Properties.GetClosestMatchProperty(kvp.Key);
                    var pp = new JsonPatchProperty(attemptedProperty, patchMember.Target);

                    EnforceMutability(operation, pp);

                    var patch = new PatchMember(kvp.Key, pp);
                    operations.AddRange(
                        AddNonDynamic(
                            kvp.Value,
                            new Operation(operation.OperationType, kvp.Key, kvp.Value),
                            patch));
                }

                return(operations);
            }

            // The second case handles the following SCIM rule:
            // o  If the target location is a multi-valued attribute and no filter
            //    is specified, the attribute and all values are replaced.
            var genericTypeOfArray = patchProperty.Property.PropertyType.GetEnumerableType();
            var conversionResult   = ConvertToActualType(genericTypeOfArray, value);

            if (!conversionResult.CanBeConverted)
            {
                throw new ScimPatchException(ScimErrorType.InvalidValue, operation);
            }

            var array = CreateGenericListFromObject(instanceValue, false);

            array.AddPossibleRange(conversionResult.ConvertedInstance);

            patchProperty.Property.ValueProvider.SetValue(
                patchProperty.Parent,
                array);

            return(new[]
            {
                new PatchOperationResult(
                    patchProperty,
                    instanceValue,
                    array)
            });
        }
Пример #3
0
        private PatchOperationResult RemoveNonDynamic(Operation operation, PatchMember patchMember)
        {
            var patchProperty = patchMember.JsonPatchProperty;

            if (!patchProperty.Property.Writable)
            {
                throw new Exception(); // TODO: (DG) This is int server error.
            }

            EnforceMutability(operation, patchProperty);

            var defaultValue  = patchProperty.Property.PropertyType.GetDefaultValue();
            var instanceValue = patchProperty.Property.ValueProvider.GetValue(patchProperty.Parent);

            if (instanceValue == defaultValue)
            {
                return(new PatchOperationResult(patchProperty, instanceValue, defaultValue));
            }

            // Here we are going to be setting or replacing a current value:
            // o  If the target location is a single-value attribute, the attribute
            //    and its associated value is removed, and the attribute SHALL be
            //    considered unassigned.
            if (!patchProperty.Property.PropertyType.IsNonStringEnumerable())
            {
                patchProperty.Property.ValueProvider.SetValue(
                    patchProperty.Parent,
                    defaultValue);

                return(new PatchOperationResult(patchProperty, instanceValue, defaultValue));
            }

            // Here we are going to be modifying an existing enumerable:
            // The first case below handles the following SCIM rule:
            // o  If the target location is a multi-valued attribute and a complex
            //    filter is specified comparing a "value", the values matched by the
            //    filter are removed.  If no other values remain after removal of
            //    the selected values, the multi-valued attribute SHALL be
            //    considered unassigned.
            // o  If the target location is a complex multi-valued attribute and a
            //    complex filter is specified based on the attribute's
            //    sub-attributes, the matching records are removed. Sub-attributes
            //    whose values have been removed SHALL be considered unassigned. If
            //    the complex multi-valued attribute has no remaining records, the
            //    attribute SHALL be considered unassigned.
            if (patchMember.Target != null &&
                (patchMember.Target is MultiValuedAttribute ||
                 !patchMember.Target.GetType().IsTerminalObject()))
            {
                var array = CreateGenericListFromObject(instanceValue);
                array.Remove(patchMember.Target);

                var newValue = array.Count == 0
                    ? defaultValue
                    : array;

                patchProperty.Property.ValueProvider.SetValue(
                    patchProperty.Parent,
                    newValue);

                return(new PatchOperationResult(patchProperty, instanceValue, newValue));
            }

            // The second case handles the following SCIM rule:
            // o  If the target location is a multi-valued attribute and no filter
            //    is specified, the attribute and all values are removed, and the
            //    attribute SHALL be considered unassigned.
            patchProperty.Property.ValueProvider.SetValue(
                patchProperty.Parent,
                defaultValue);

            return(new PatchOperationResult(patchProperty, instanceValue, defaultValue));
        }
Пример #4
0
        private IEnumerable <PatchOperationResult> AddNonDynamic(object value, Operation operation, PatchMember patchMember)
        {
            var patchProperty = patchMember.JsonPatchProperty;

            EnforceMutability(operation, patchProperty);

            var instanceValue = patchProperty.Property.ValueProvider.GetValue(patchProperty.Parent);

            if (instanceValue == null || !patchProperty.Property.PropertyType.IsNonStringEnumerable())
            {
                /*
                 *  Here we are going to be setting or replacing a current value:
                 *      o  If the target location does not exist, the attribute and value are added.
                 *          (instanceValue == null)
                 *      o  If the target location specifies a single-valued attribute, the existing value is replaced.
                 *          (!patchProperty.Property.PropertyType.IsNonStringEnumerable())
                 *      o  If the target location exists, the value is replaced.
                 *          (!patchProperty.Property.PropertyType.IsNonStringEnumerable())
                 */

                var conversionResultTuple = ConvertToActualType(
                    patchProperty.Property.PropertyType,
                    value);

                if (!conversionResultTuple.CanBeConverted)
                {
                    throw new ScimPatchException(
                              ScimErrorType.InvalidValue,
                              operation);
                }

                if (!patchProperty.Property.Writable)
                {
                    throw new Exception(); // TODO: (DG) This is int server error.
                }

                patchProperty.Property.ValueProvider.SetValue(
                    patchProperty.Parent,
                    conversionResultTuple.ConvertedInstance);

                return(new[]
                {
                    new PatchOperationResult(
                        patchProperty,
                        instanceValue,
                        conversionResultTuple.ConvertedInstance)
                });
            }

            /*
             *      o  If the target location already contains the value specified, no
             *          changes SHOULD be made to the resource, and a success response
             *          SHOULD be returned.  Unless other operations change the resource,
             *          this operation SHALL NOT change the modify timestamp of the
             *          resource.
             */

            // The first case below handles the following SCIM rule:
            // o  If the target location specifies a complex attribute, a set of
            //    sub - attributes SHALL be specified in the "value" parameter.
            if (patchMember.Target != null &&
                (patchMember.Target is MultiValuedAttribute ||
                 !patchMember.Target.GetType().IsTerminalObject()))
            {
                // value should be an object composed of sub-attributes of the parent, a MultiValuedAttribute
                var operations    = new List <PatchOperationResult>();
                var resourcePatch = JObject.Parse(operation.Value.ToString());
                var jsonContract  = (JsonObjectContract)ContractResolver.ResolveContract(patchMember.Target.GetType());
                foreach (var kvp in resourcePatch)
                {
                    var attemptedProperty = jsonContract.Properties.GetClosestMatchProperty(kvp.Key);
                    var pp    = new JsonPatchProperty(attemptedProperty, patchMember.Target);
                    var patch = new PatchMember(kvp.Key, pp);

                    EnforceMutability(operation, pp);

                    operations.AddRange(
                        AddNonDynamic(
                            kvp.Value,
                            new Operation(operation.OperationType, kvp.Key, kvp.Value),
                            patch));
                }

                return(operations);
            }

            // Here we are going to be modifying an existing enumerable:
            // The second case handles the following SCIM rule:
            // o If the target location specifies a multi-valued attribute, a new
            //   value is added to the attribute.
            var genericTypeOfArray = patchProperty.Property.PropertyType.GetEnumerableType();
            var conversionResult   = ConvertToActualType(genericTypeOfArray, value);

            if (!conversionResult.CanBeConverted)
            {
                throw new ScimPatchException(ScimErrorType.InvalidValue, operation);
            }

            var array = CreateGenericListFromObject(instanceValue);

            array.AddPossibleRange(conversionResult.ConvertedInstance);

            patchProperty.Property.ValueProvider.SetValue(
                patchProperty.Parent,
                array);

            return(new[]
            {
                new PatchOperationResult(
                    patchProperty,
                    instanceValue,
                    array)
            });
        }