diff --git a/app/finders/users_finder.rb b/app/finders/users_finder.rb index 07deceb827bb7999ac338b573729c8616eef73ed..ae0311628924a61f4be3202cfccaab2d960f691d 100644 --- a/app/finders/users_finder.rb +++ b/app/finders/users_finder.rb @@ -29,6 +29,7 @@ class UsersFinder users = by_active(users) users = by_external_identity(users) users = by_external(users) + users = by_created_at(users) users end @@ -71,4 +72,16 @@ class UsersFinder users.external end + + def by_created_at(users) + if params[:created_after].present? + users = users.where(users.klass.arel_table[:created_at].gteq(params[:created_after])) + end + + if params[:created_before].present? + users = users.where(users.klass.arel_table[:created_at].lteq(params[:created_before])) + end + + users + end end diff --git a/lib/api/users.rb b/lib/api/users.rb index 88bca235692f66b10638ec0c6cf15e4057515c72..47a44ba9598e7d8857df562d4941883d23716ca1 100644 --- a/lib/api/users.rb +++ b/lib/api/users.rb @@ -48,6 +48,8 @@ module API optional :active, type: Boolean, default: false, desc: 'Filters only active users' optional :external, type: Boolean, default: false, desc: 'Filters only external users' optional :blocked, type: Boolean, default: false, desc: 'Filters only blocked users' + optional :created_after, type: DateTime, desc: 'Return users created after the specified time' + optional :created_before, type: DateTime, desc: 'Return users created before the specified time' all_or_none_of :extern_uid, :provider use :pagination @@ -55,6 +57,10 @@ module API get do authenticated_as_admin! if params[:external].present? || (params[:extern_uid].present? && params[:provider].present?) + unless current_user.admin? + params.except!(:created_after, :created_before) + end + users = UsersFinder.new(current_user, params).execute authorized = can?(current_user, :read_users_list) diff --git a/spec/finders/users_finder_spec.rb b/spec/finders/users_finder_spec.rb index a2e5081b8d3be5d888506af18a132640c2bd7b0a..a5e4a39a695bafaa2263851a393fda38e1d7f322 100644 --- a/spec/finders/users_finder_spec.rb +++ b/spec/finders/users_finder_spec.rb @@ -47,7 +47,9 @@ describe UsersFinder do end it 'filters by created_at' do - users = described_class.new(user, created_after: 2.days.ago, created_before: Time.now + 2.days).execute + users = described_class.new(user, + created_after: 2.days.ago, + created_before: Time.now + 2.days).execute expect(users.count).to eq(4) end