concord-consortium/rigse

View on GitHub
rails/lib/tasks/api.rake

Summary

Maintainability
Test Coverage
namespace :api do
  desc "Create API admin user with a client entry and token grant for 10 years"
  task :create_admin_user => :environment do

    # create the user if needed
    password = rand(36**16).to_s(36)
    api_user = User.where(:login => "admin_api_user").first_or_create(
      :first_name            => "Admin API",
      :last_name             => "User",
      :email                 => "admin_api_user@concord.org",
      :password              => password,
      :password_confirmation => password){|u| u.skip_notifications = true}
    api_user.confirm
    api_user.add_role("admin")

    # create the client if needed
    client = Client.where(:name => "admin_api_user_client").first_or_create(
      :app_id                => "admin_api_user_client",
      :app_secret            => SecureRandom.uuid
    )

    # create the access token
    access_grant = api_user.access_grants.create({:client => client, :state => nil})
    access_grant.update(:access_token_expires_at => Time.now + 10.year)

    puts "Access token: #{access_grant.access_token} (valid until #{access_grant.access_token_expires_at})"
  end
end