Class: Rubix::User

Inherits:
Model
  • Object
show all
Includes:
Associations::HasManyUserGroups
Defined in:
lib/rubix/models/user.rb

Constant Summary collapse

TYPE_CODES =

Numeric codes for the various user types.

{
  :normal      => 1,
  :admin       => 2,
  :super_admin => 3
}.freeze
TYPE_NAMES =
TYPE_CODES.invert.freeze

Instance Attribute Summary collapse

Attributes inherited from Model

#id, #properties

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Associations::HasManyUserGroups

#user_group_ids, #user_group_ids=, #user_groups, #user_groups=

Methods inherited from Model

all, all_params, all_request, #before_destroy, #create, #create_request, #destroy, #destroy_params, #destroy_request, each, find, find_or_create, find_request, id_field, #id_field, list, #new_record?, properties, request, #request, #resource_name, resource_name, #save, #to_hash, #update, #update_params, #update_request, web_request, zabbix_attr, zabbix_name

Methods included from Logs

#debug, #error, #fatal, #info, #warn

Constructor Details

#initialize(properties = {}) ⇒ User

Returns a new instance of User.



34
35
36
37
38
39
40
41
42
# File 'lib/rubix/models/user.rb', line 34

def initialize properties={}
  super(properties)
  self.password_md5 = properties[:password_md5]

  self.user_group_ids = properties[:user_group_ids]
  self.user_groups    = properties[:user_groups]

  self.media          = properties[:media]
end

Instance Attribute Details

#mediaObject

Returns the value of attribute media.



59
60
61
# File 'lib/rubix/models/user.rb', line 59

def media
  @media
end

#password_md5Object

Returns the value of attribute password_md5.



32
33
34
# File 'lib/rubix/models/user.rb', line 32

def password_md5
  @password_md5
end

Class Method Details

.build(user) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rubix/models/user.rb', line 117

def self.build user
  new({
        :id                 => user[id_field].to_i,
        :username           => user['alias'],
        :first_name         => user['name'],
        :last_name          => user['surname'],
        :password_md5       => user['passwd'],
        :url                => user['url'],
        :auto_login         => (user['autologin'].to_i == 1),
        :auto_logout_period => user['autologout'],
        :lang               => user['lang'],
        :refresh_period     => user['refresh'].to_i,
        :type               => self::TYPE_NAMES[user['type'].to_i],
        :theme              => user['theme'],
        :rows_per_page      => user['rows_per_page'].to_i
      })
end

.find_params(options = {}) ⇒ Object



140
141
142
# File 'lib/rubix/models/user.rb', line 140

def self.find_params options={}
  get_params.merge(:filter => {:alias => options[:username], id_field => options[:id]})
end

.get_paramsObject



135
136
137
138
# File 'lib/rubix/models/user.rb', line 135

def self.get_params
  # FIXME -- select_medias doesn't seem to work here...
  super().merge({})
end

Instance Method Details

#after_createObject



98
99
100
# File 'lib/rubix/models/user.rb', line 98

def after_create
  update_media
end

#before_updateObject



102
103
104
# File 'lib/rubix/models/user.rb', line 102

def before_update
  update_media
end

#create_paramsObject

Requests ==



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rubix/models/user.rb', line 74

def create_params
  {
    :alias         => username,
    :name          => first_name,
    :surname       => last_name,
    :url           => url,
    :lang          => lang,
    :refresh       => refresh_period,
    :type          => self.class::TYPE_CODES[type],
    :theme         => theme,
    :rows_per_page => rows_per_page
  }.tap do |cp|
    cp[:passwd] = password if password && (!password.empty?)
    
    case
    when 
      cp[:autologin] = 1
    when (!auto_logout_period.nil?)
      cp[:autologout] = auto_logout_period
    end
    
  end
end

#update_mediaObject



106
107
108
109
110
111
112
113
114
115
# File 'lib/rubix/models/user.rb', line 106

def update_media
  return true if media.nil?
  response = request("user.updateMedia", { :users => [{:userid => id}], :medias => media.map(&:to_hash) })
  if response.has_data?
    true
  else
    error("Could not update media for #{resource_name}: #{response.error_message}")
    false
  end
end

#validateObject

Validations ==

Raises:



47
48
49
50
51
# File 'lib/rubix/models/user.rb', line 47

def validate
  super()
  raise ValidationError.new("A new user must have a password") if new_record? && (password.nil? || password.empty?)
  true
end