[ProducesResponseType(typeof(void), 404)] // Not Found
        //[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme + ",BasicAuthentication", Roles = "Admin")]
        public async Task <IActionResult> PostUserRef([FromRoute] int id, [FromBody] ODataReference reference)
        {
            try {
                Address address = await _db.Addresses.FindAsync(id);

                if (address == null)
                {
                    return(NotFound());
                }
                var  userId = Convert.ToInt32(ReferenceHelper.GetKeyFromUrl(reference.uri));
                User user   = await _db.Users.FindAsync(userId);

                if (user == null)
                {
                    return(NotFound());
                }
                address.Users.Add(user);
                await _db.SaveChangesAsync();

                return(NoContent());
            } catch (Exception ex) {
                if (ex.InnerException.Message.Contains(Constants.errorSqlDuplicateKey))
                {
                    return(Conflict(Constants.messageDupAssoc + Constants.messageAppInsights));
                }
                else
                {
                    return(StatusCode(500, ex.Message + Constants.messageAppInsights));
                }
            }
        }
示例#2
0
        internal static object GetJsonObject(Field field, string selfUrl, ODataRequest oDataRequest)
        {
            object data;

            if (field is ReferenceField)
            {
                return(ODataReference.Create(String.Concat(selfUrl, "/", field.Name)));
            }
            else if (field is BinaryField binaryField)
            {
                try
                {
                    // load binary fields only if the content is finalized
                    var binaryData = field.Content.ContentHandler.SavingState == ContentSavingState.Finalized
                        ? (BinaryData)binaryField.GetData()
                        : null;

                    return(ODataBinary.Create(BinaryField.GetBinaryUrl(binaryField.Content.Id, binaryField.Name, binaryData?.Timestamp ?? default),
                                              null, binaryData?.ContentType, null));
                }
                catch (Exception ex)
                {
                    SnTrace.System.WriteError(
                        $"Error accessing field {field.Name} of {field.Content.Path} with user {User.Current.Username}: " +
                        ex.Message);

                    return(null);
                }
            }
            else if (ODataMiddleware.DeferredFieldNames.Contains(field.Name))
            {
                return(ODataReference.Create(String.Concat(selfUrl, "/", field.Name)));
            }
            try
            {
                data = field.GetData();
            }
            catch (SenseNetSecurityException)
            {
                // The user does not have access to this field (e.g. cannot load
                // a referenced content). In this case we serve a null value.
                data = null;

                SnTrace.Repository.Write("PERMISSION warning: user {0} does not have access to field '{1}' of {2}.", User.LoggedInUser.Username, field.Name, field.Content.Path);
            }

            if (data is NodeType nodeType)
            {
                return(nodeType.Name);
            }
            if (data is RichTextFieldValue rtfValue)
            {
                return(GetRichTextOutput(field.Name, rtfValue, oDataRequest));
            }
            return(data);
        }
示例#3
0
        //TODO: Bad name: GetJsonObject is a method for odata serializing
        internal static object GetJsonObject(Field field, string selfUrl)
        {
            object data;

            if (field is ReferenceField)
            {
                return(ODataReference.Create(String.Concat(selfUrl, "/", field.Name)));
            }
            else if (field is BinaryField)
            {
                var binaryField = (BinaryField)field;
                var binaryData  = (BinaryData)binaryField.GetData();

                return(ODataBinary.Create(BinaryField.GetBinaryUrl(field.Content.Id, field.Name, binaryData.Timestamp), null, binaryData.ContentType, null));
            }
            else if (ODataMiddleware.DeferredFieldNames.Contains(field.Name))
            {
                return(ODataReference.Create(String.Concat(selfUrl, "/", field.Name)));
            }
            try
            {
                data = field.GetData();
            }
            catch (SenseNetSecurityException)
            {
                // The user does not have access to this field (e.g. cannot load
                // a referenced content). In this case we serve a null value.
                data = null;

                SnTrace.Repository.Write("PERMISSION warning: user {0} does not have access to field '{1}' of {2}.", User.LoggedInUser.Username, field.Name, field.Content.Path);
            }

            if (data is NodeType nodeType)
            {
                return(nodeType.Name);
            }
            return(data);
        }