Class: Rubix::User

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

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, #save, #to_hash, #update, #update_params, #update_request, web_request, zabbix_attr, zabbix_define, zabbix_name

Methods included from Logs

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

Constructor Details

#initialize(properties = {}) ⇒ User

Returns a new instance of User.



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

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.



63
64
65
# File 'lib/rubix/models/user.rb', line 63

def media
  @media
end

#password_md5Object

Returns the value of attribute password_md5.



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

def password_md5
  @password_md5
end

Class Method Details

.build(user) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rubix/models/user.rb', line 121

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



144
145
146
# File 'lib/rubix/models/user.rb', line 144

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

.get_paramsObject



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

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

Instance Method Details

#after_createObject



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

def after_create
  update_media
end

#before_updateObject



106
107
108
# File 'lib/rubix/models/user.rb', line 106

def before_update
  update_media
end

#create_paramsObject

Requests ==



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rubix/models/user.rb', line 78

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

#resource_nameObject



43
44
45
# File 'lib/rubix/models/user.rb', line 43

def resource_name
  "#{self.class.resource_name} #{self.username || self.id}"
end

#update_mediaObject



110
111
112
113
114
115
116
117
118
119
# File 'lib/rubix/models/user.rb', line 110

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:



51
52
53
54
55
# File 'lib/rubix/models/user.rb', line 51

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