Project

General

Profile

Download (2.62 KB) Statistics
| Branch: | Tag: | Revision:
ba69b49b Martin Bačovský
module Api
module V1
d076d573 Joseph Mitchell Magen
class UsersController < V1::BaseController
355bce36 Ohad Levy
include Foreman::Controller::UsersMixin

ba69b49b Martin Bačovský
before_filter :find_resource, :only => %w{show update destroy}

api :GET, "/users/", "List all users."
3398b5bd Petr Chalupa
param :search, String, :desc => "filter results"
d076d573 Joseph Mitchell Magen
param :order, String, :desc => "sort results"
param :page, String, :desc => "paginate results"
param :per_page, String, :desc => "number of entries per request"

ba69b49b Martin Bačovský
def index
d076d573 Joseph Mitchell Magen
@users = User.search_for(*search_options).paginate(paginate_options)
ba69b49b Martin Bačovský
end

api :GET, "/users/:id/", "Show an user."
3398b5bd Petr Chalupa
param :id, String, :required => true
d076d573 Joseph Mitchell Magen
ba69b49b Martin Bačovský
def show
3398b5bd Petr Chalupa
@user
ba69b49b Martin Bačovský
end

api :POST, "/users/", "Create an user."
bfbf7ed8 Lukas Zapletal
# TRANSLATORS: API documentation - do not translate
ba69b49b Martin Bačovský
description <<-DOC
Adds role 'Anonymous' to the user by default
DOC
param :user, Hash, :required => true do
1548ca7a Petr Chalupa
param :login, String, :required => true
param :firstname, String, :required => false
param :lastname, String, :required => false
param :mail, String, :required => true
param :admin, :bool, :required => false, :desc => "Is an admin account?"
param :password, String, :required => true
param :auth_source_id, Integer, :required => true
ba69b49b Martin Bačovský
end
d076d573 Joseph Mitchell Magen
ba69b49b Martin Bačovský
def create
if @user.save
process_success
else
process_resource_error
end
end

api :PUT, "/users/:id/", "Update an user."
bfbf7ed8 Lukas Zapletal
# TRANSLATORS: API documentation - do not translate
ba69b49b Martin Bačovský
description <<-DOC
Adds role 'Anonymous' to the user if it is not already present.
Only admin can set admin account.
DOC
3398b5bd Petr Chalupa
param :id, String, :required => true
ba69b49b Martin Bačovský
param :user, Hash, :required => true do
73f028b0 Joseph Mitchell Magen
param :login, String
param :firstname, String, :allow_nil => true
param :lastname, String, :allow_nil => true
param :mail, String
param :admin, :bool, :desc => "Is an admin account?"
param :password, String
ba69b49b Martin Bačovský
end
d076d573 Joseph Mitchell Magen
ba69b49b Martin Bačovský
def update
if @user.update_attributes(params[:user])
355bce36 Ohad Levy
update_sub_hostgroups_owners

ba69b49b Martin Bačovský
process_success
else
process_resource_error
end
end

api :DELETE, "/users/:id/", "Delete an user."
3398b5bd Petr Chalupa
param :id, String, :required => true
d076d573 Joseph Mitchell Magen
ba69b49b Martin Bačovský
def destroy
if @user == User.current
deny_access "You are trying to delete your own account"
else
process_response @user.destroy
end
end
3398b5bd Petr Chalupa
df4c1afc Dominic Cleal
protected
def resource_identifying_attributes
%w(id login)
end
3398b5bd Petr Chalupa
df4c1afc Dominic Cleal
end
ba69b49b Martin Bačovský
end
end