Class: CortexReaver::User

Inherits:
Object
  • Object
show all
Defined in:
lib/cortex_reaver/model/user.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.anonymousObject

An anonymous proxy user, with no permissions.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cortex_reaver/model/user.rb', line 36

def self.anonymous
  # Return singleton if stored
  return @anonymous_user if @anonymous_user

  # Create anonymous user
  @anonymous_user = self.new(:name => "Anonymous")

  # These functions are embedded for speed. Much faster public browsing!
  def @anonymous_user.can_create? other
    false
  end
  def @anonymous_user.can_edit? other
    false
  end
  def @anonymous_user.can_delete? other
    false
  end
  def @anonymous_user.anonymous?
    true
  end
  
  @anonymous_user
end

.anonymous?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/cortex_reaver/model/user.rb', line 53

def @anonymous_user.anonymous?
  true
end

.authenticate(login, password) ⇒ Object

Returns an authenticated user by login and password, or nil.



26
27
28
29
30
31
32
33
# File 'lib/cortex_reaver/model/user.rb', line 26

def self.authenticate(, password)
  user = self[:login => ]
  if user and user.authenticate(password)
    user
  else
    nil
  end
end

.can_create?(other) ⇒ Boolean

These functions are embedded for speed. Much faster public browsing!

Returns:

  • (Boolean)


44
45
46
# File 'lib/cortex_reaver/model/user.rb', line 44

def @anonymous_user.can_create? other
  false
end

.can_delete?(other) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/cortex_reaver/model/user.rb', line 50

def @anonymous_user.can_delete? other
  false
end

.can_edit?(other) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/cortex_reaver/model/user.rb', line 47

def @anonymous_user.can_edit? other
  false
end

.canonical_name_attrObject

CRUD uses this to construct URLs. Even though we don’t need the full power of Canonical, CRUD is pretty useful. :)



62
63
64
# File 'lib/cortex_reaver/model/user.rb', line 62

def self.canonical_name_attr
  :login
end

.get(id) ⇒ Object

Get a user



67
68
69
# File 'lib/cortex_reaver/model/user.rb', line 67

def self.get(id)
  self[:login => id] || self[id]
end

.urlObject

Class URL



72
73
74
# File 'lib/cortex_reaver/model/user.rb', line 72

def self.url
  '/users'
end

Instance Method Details

#admin?Boolean

Returns true if the user is an administrator.

Returns:

  • (Boolean)


77
78
79
# File 'lib/cortex_reaver/model/user.rb', line 77

def admin?
  self.admin
end

#anonymous?Boolean

Is this the special anonymous user?

Returns:

  • (Boolean)


21
22
23
# File 'lib/cortex_reaver/model/user.rb', line 21

def anonymous?
  false
end

#authenticate(test_password) ⇒ Object

Authenticate with password



82
83
84
85
86
87
88
# File 'lib/cortex_reaver/model/user.rb', line 82

def authenticate(test_password)
  if self[:password] == self.class.crypt(test_password, self.salt)
    true
  else
    false
  end
end

#before_destroyObject

Ensure that we don’t destroy the only admin.



91
92
93
94
95
96
97
98
# File 'lib/cortex_reaver/model/user.rb', line 91

def before_destroy
  return false if super == false

  if admins = User.filter(:admin => true) and admins.count == 1 and admins.first.id == self.id
    self.errors.add nil, "Can't destroy the only administrator."
    return false
  end
end

#can_create?(other) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/cortex_reaver/model/user.rb', line 100

def can_create?(other)
  if admin?
    # Administrators may create anything
    true
  elsif contributor?
    # Contributors may create anything but users
    case other
    when User
      false
    else
      true
    end
  else
    # Anyone may create a comment.
    case other
    when Comment
      true
    else
      false
    end
  end
end

#can_delete?(other) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/cortex_reaver/model/user.rb', line 123

def can_delete?(other)
  if admin?
    # Administrators may delete anything
    true
  elsif other.respond_to? :created_by and other.created_by == self.id
    # Anybody may delete their own records.
    true
  elsif editor? and not User === other
    # Editors may delete anything but users.
    true
  elsif moderator? and Comment === other
    # Moderators may delete comments.
    true
  else
    false
  end
end

#can_edit?(other) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/cortex_reaver/model/user.rb', line 141

def can_edit?(other)
  if admin?
    # Administrators may edit anything
    true
  elsif other.respond_to? :created_by and other.created_by == self.id
    # Anybody may edit their own records
    true
  elsif editor? and not User === other
    # Editors may edit anything but other users.
    true
  elsif moderator and Comment === other
    # Moderators may edit comments
    true
  else
    false
  end
end

#can_view?(other) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/cortex_reaver/model/user.rb', line 159

def can_view?(other)
  if other.respond_to? :draft and other.draft
    # Draft
    if admin? or can_edit? other
      # User can edit this draft
      true
    else
      # Nope, not yet!
      false
    end
  else
    # Not a draft
    true
  end
end

#contributor?Boolean

Returns true if user is a contributor

Returns:

  • (Boolean)


176
177
178
# File 'lib/cortex_reaver/model/user.rb', line 176

def contributor?
  self.contributor
end

#editor?Boolean

Returns true if user is an editor

Returns:

  • (Boolean)


181
182
183
# File 'lib/cortex_reaver/model/user.rb', line 181

def editor?
  self.editor
end

#moderator?Boolean

Returns true if user is a moderator

Returns:

  • (Boolean)


186
187
188
# File 'lib/cortex_reaver/model/user.rb', line 186

def moderator?
  self.moderator
end

#nameObject

Name falls back to login if blank



191
192
193
194
# File 'lib/cortex_reaver/model/user.rb', line 191

def name
  name = self[:name]
  name.blank? ?  : name
end

#password=(password) ⇒ Object

Set user password



197
198
199
200
201
# File 'lib/cortex_reaver/model/user.rb', line 197

def password=(password)
  self.salt ||= self.class.new_salt
  self[:password] = self.class.crypt(password, self.salt)
  @password_length = '*' * password.length
end

#password_confirmationObject



209
210
211
212
213
214
# File 'lib/cortex_reaver/model/user.rb', line 209

def password_confirmation
  # If password_confirmation was set, use that. Otherwise, fall back
  # to the normal password, so we don't need set the confirmation every
  # time the password is updated programmatically.
  @password_confirmation || self.password
end

#password_confirmation=(password) ⇒ Object

Password confirmation



204
205
206
207
# File 'lib/cortex_reaver/model/user.rb', line 204

def password_confirmation=(password)
  self.salt ||= self.class.new_salt
  @password_confirmation = self.class.crypt(password, self.salt)
end

#password_lengthObject

A cache for password length, so we can validate without keeping the password as plaintext.



218
219
220
# File 'lib/cortex_reaver/model/user.rb', line 218

def password_length
  @password_length
end

#to_sObject



222
223
224
225
226
227
228
# File 'lib/cortex_reaver/model/user.rb', line 222

def to_s
  if name.blank?
    
  else
    name
  end
end

#urlObject

A URL to view this user



231
232
233
# File 'lib/cortex_reaver/model/user.rb', line 231

def url
  '/users/show/' + 
end

#validateObject



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/cortex_reaver/model/user.rb', line 235

def validate
  validates_unique(:login, :message => "Already taken.")
  validates_max_length(255, :login, :message => "Please enter a username shorter than 255 characters.")
  validates_format(/^[A-Za-z0-9\-_]+$/, :login, :message => "Logins can only contain alphanumeric characters, dashes, and underscores.")
  validates_max_length(255, :name, :allow_blank => true, :message => "Please enter a name shorter than 255 characters.")
  validates_max_length(255, :http, :allow_blank => true, :message => "Please enter an HTTP address shorter than 255 characters.")
  validates_unique(:email, :message => "Already taken.")
  validates_max_length(255, :email, :message => "Please enter an email address shorter than 255 characters.")
  validates_format(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/, :email, :message => "Please enter a valid email address.")
  validates_confirmation(:password, :message => "Make sure your passwords match.")
  validates_min_length(8, :password_length, :message => "Passwords must be at least 8 characters.", :allow_nil => true)
  validates_max_length(255, :password_length, :message => "Passwords must be at most 255 characters.", :allow_nil => true)

  # Ensure an administrator is always available.
  if admins = User.filter(:admin => true) and admins.count == 1 and admins.first.id == self.id and not admin?
    errors[:admin] << "can't be unset; only one administrator left!"
  end
end