Class: UserManagement::UsersController

Inherits:
ApplicationController show all
Defined in:
app/controllers/user_management/users_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

authorized, decode_token, #encode_token, logged_in?, logged_in_user

Instance Method Details

#add_privilegeObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/user_management/users_controller.rb', line 61

def add_privilege
    user = User.find(params[:id]) rescue nil
    if user.nil?
        render json: {message: 'No user with id #{params[:id]}'}, status: :no_content
    else
        privilege_id = Privilege.where(name: params[:name]).first.id
        user_privilege = UserPrivilege.new(user_id: user.id, privilege_id: privilege_id, active: 1 )
        if user_privilege.save
            render json: user_privilege, status: :created
        else
            render json: user_privilege.error, status: :unprocessable_entity
        end
    end
end

#createObject



8
9
10
11
12
13
14
15
16
# File 'app/controllers/user_management/users_controller.rb', line 8

def create
    person = Person.create(first_name: params[:first_name],surname: params[:surname], sex: params[:sex])
    user = User.new(username: params[:username], email: params[:email], person_id:person.id, password:params[:password],password_confirmation: params[:password_confirmation])
    if user.save
        render json: user, status: :created
    else
        render json: user.error, status: :unprocessable_entity
    end
end

#delete_privilegeObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/user_management/users_controller.rb', line 76

def delete_privilege
    user = User.find(params[:id]) rescue nil
    privilege_id = Privilege.where(name: params[:name]).first.id
    user_privilege = UserPrivilege.where(user_id: user.id, privilege_id: privilege_id, active: 1 ).first rescue nil
    if user.nil? || user_privilege.nil?
        render json: {message: 'No user or privilege with id #{params[:id]}'}, status: :no_content
    else
        user_privilege.active = 1
        if user_privilege.save
            render json: user_privilege, status: :accepted
        else
            render json: user_privilege.error, status: :unprocessable_entity
        end
    end
end

#destroyObject



51
52
53
54
55
56
57
58
59
# File 'app/controllers/user_management/users_controller.rb', line 51

def destroy
    user = User.find(params[:id])
    if user.destroy!
        render json: {message: 'Record Deleted'}, status: :no_content

    else
        render json: user.error, status: :unprocessable_entity
    end
end

#indexObject



5
6
7
# File 'app/controllers/user_management/users_controller.rb', line 5

def index
    render json: User.all ,status: 200
end

#showObject



18
19
20
21
22
23
24
25
# File 'app/controllers/user_management/users_controller.rb', line 18

def show
    user = User.find(params[:id]) rescue nil
    if user.nil?
        render json: {message: 'No user with id #{params[:id]}'}, status: :no_content
    else
        render json: user, status: 200
    end
end

#updateObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/user_management/users_controller.rb', line 27

def update
    user = User.find(params[:id]) rescue nil
    person = Person.find(user.person_id)
    if user.nil?
        render json: {message: 'No user with id #{params[:id]}'}, status: :no_content
    else
        params.keys.each do |key|
            next if ["username","email","password"].exclude?(key)
            user[key] = params[key]
        end

        params.keys.each do |key|
            next if ["first_name","surname","sex"].exclude?(key)
            person[key] = params[key]
        end

        if user.save && person.save
            render json: user, status: 200
        else
            render json: user.error, status: :unprocessable_entity
        end
    end
end