diff options
| author | Sean McGivern <sean@mcgivern.me.uk> | 2017-08-30 09:56:17 +0000 |
|---|---|---|
| committer | Sean McGivern <sean@mcgivern.me.uk> | 2017-08-30 09:56:17 +0000 |
| commit | f11049ab9131b78c14e95bd95b3073c1eaf59392 (patch) | |
| tree | 882c763637a02a386486799c39897bd563c7b6aa /lib/api/helpers.rb | |
| parent | a16854ff462935f1a967f31abfeb242cf26284cc (diff) | |
| parent | ee4820a5268d02fb7ed142511d1a194f06629a1e (diff) | |
| download | gitlab-ce-f11049ab9131b78c14e95bd95b3073c1eaf59392.tar.gz | |
Merge branch 'api-delete-respect-headers' into 'master'
API: Respect the 'If-Unmodified-Since' for delete endpoints
See merge request !9621
Diffstat (limited to 'lib/api/helpers.rb')
| -rw-r--r-- | lib/api/helpers.rb | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index b56fd2388b3..84980864151 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -11,6 +11,25 @@ module API declared(params, options).to_h.symbolize_keys end + def check_unmodified_since!(last_modified) + if_unmodified_since = Time.parse(headers['If-Unmodified-Since']) rescue nil + + if if_unmodified_since && last_modified && last_modified > if_unmodified_since + render_api_error!('412 Precondition Failed', 412) + end + end + + def destroy_conditionally!(resource, last_update_field: :updated_at) + check_unmodified_since!(resource.public_send(last_update_field)) + + status 204 + if block_given? + yield resource + else + resource.destroy + end + end + def current_user return @current_user if defined?(@current_user) |
