Guest User

Untitled

a guest
Mar 8th, 2018
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.18 KB | None | 0 0
  1. class User < Helene::Sdb::Base
  2.  
  3. ##############
  4. ### Schema ###
  5. ##############
  6.  
  7. attribute :email, :type => :string, :null => false
  8. attribute :password, :type => :encrypted, :null => false
  9. attribute :role, :type => :string, :default => "user"
  10. attribute :account_plan, :type => :string
  11.  
  12. attribute :first_name, :type => :string
  13. attribute :last_name, :type => :string
  14. attribute :website, :type => :url
  15. attribute :company, :type => :string
  16. attribute :description, :type => :string
  17.  
  18. ####################
  19. ### Associations ###
  20. ####################
  21.  
  22. has_many :inbox, :class_name => "Message", :foreign_key => :dst_id
  23. has_many :outbox, :class_name => "Message", :foreign_key => :src_id
  24.  
  25. #######################
  26. ### Specializations ###
  27. #######################
  28.  
  29. class Activation < User
  30. attr_accessor :email_confirmation
  31. attr_accessor :password_confirmation
  32. attr_accessor :token
  33.  
  34. def self.class_for(attributes)
  35. User
  36. end
  37.  
  38. # validates_presence_of :email
  39. # validates_presence_of :password
  40. # validates_presence_of :token, :message => 'a valid signup token is required to activate'
  41. # validates_confirmation_of :email, :message => 'is not the address used for signup'
  42. # validates_confirmation_of :password
  43. #
  44. # validates_each :token do |record, attr, value|
  45. # if value
  46. # token_data = TokenData[value]
  47. # email = token_data[:email]
  48. # record.errors_add attr, 'invalid token' if record.email != email
  49. # #email = Encryption.decrypt(value)
  50. # end
  51. # end
  52.  
  53. def build_credit_card
  54. nil
  55. end
  56. end
  57.  
  58. #####################
  59. ### Class Methods ###
  60. #####################
  61.  
  62. Fattr :current
  63.  
  64. def self.authenticate(*args)
  65. options = args.options
  66. args.flatten!
  67.  
  68. email = options.getopt(:email, args.shift).to_s.downcase.strip
  69. password = options.getopt(:password, args.shift).to_s.strip
  70.  
  71. current_controller = ActionController.current
  72. local = options.getopt(:local, current_controller && current_controller.send('local_request?')) rescue false
  73.  
  74. users = User.find(:all, :conditions => {:email => email})
  75. return nil if users.empty?
  76.  
  77. if users.size > 1
  78. Alert.alert(:duplicate_email, "#{email} is duplicated in the system")
  79. return false
  80. end
  81.  
  82. user = users.first
  83. if local or user.password == password
  84. user
  85. else
  86. false
  87. end
  88. end
  89.  
  90. def self.find_by_email(email)
  91. find(:first, :conditions => {:email => email})
  92. end
  93.  
  94. ########################
  95. ### Instance Methods ###
  96. ########################
  97.  
  98. def password
  99. pass = Array(attributes[:password]).first
  100. Encryption.decrypt(pass) rescue pass
  101. end
  102.  
  103. def signup_token
  104. @signup_token ||= TokenData.create!(:data => {:email => email})
  105. end
  106.  
  107. def name
  108. if(!first_name.blank? || !last_name.blank?)
  109. [first_name, last_name].join(' ').strip
  110. else
  111. email
  112. end
  113. end
  114.  
  115. def shared_orders_with(user)
  116. [] # eventually this will be real once we can add other users to your order
  117. end
  118.  
  119. def shared_dropboxes_with(user)
  120. # do we need to also add dropboxes where both user_ids are added on a third person's dropbox?
  121. db = []
  122. # db << dropboxes.find(:all, :joins => :dropbox_user_joins, :conditions => {"#{ DropboxUserJoin.table_name }.user_id" => user.id})
  123. # db << user.dropboxes.find(:all, :joins => :dropbox_user_joins, :conditions => {"#{ DropboxUserJoin.table_name }.user_id" => id})
  124. db.flatten
  125. end
  126.  
  127. def message other, content, options = {}
  128. options.to_options!
  129. user = self
  130. options.reverse_merge!(:src_id => user.id, :dst_id => other.id, :content => content.to_s.strip)
  131. Message.create!(options)
  132. end
  133.  
  134. # def inbox options = {}, &block
  135. # options.to_options!
  136. # user = self
  137. # Message.inbox(user, options, &block)
  138. # end
  139. #
  140. # def outbox(*args, &block)
  141. # Message.outbox(user=self, *args, &block)
  142. # end
  143.  
  144. def request_connection_to(user, message)
  145. message(user, message, :message_type => :connection)
  146. end
  147.  
  148. def pending_connection_requests
  149. Message.find(:all, :conditions => {:message_type => :connection, :viewed => false, :src_id => self.id})
  150. end
  151.  
  152. def user_connections
  153. UserConnection.find(:all, :conditions => [
  154. {:accepter_id => self.id},
  155. 'or',
  156. {:requester_id => self.id}
  157. ])
  158. end
  159.  
  160. # u = User.last
  161. # a = User.first
  162. # cu = a.connected_users
  163. # cu.map(&:first_name)
  164. #sql_for_select
  165. def connected_users
  166. ids = connected_user_ids
  167. return [] if ids.empty?
  168.  
  169. puts "Calling User.find with these ids #{ids.join(', ')}"
  170. User.find(ids)
  171. end
  172.  
  173. def connected_user_ids
  174. accepted_connections = UserConnection.find(:all, :conditions => {:accepter_id => self.id})
  175. puts "Found #{accepted_connections.size} accepted connections"
  176. requested_connections = UserConnection.find(:all, :conditions => {:requester_id => self.id})
  177. puts "Found #{requested_connections.size} requested connections"
  178. accepted_connections.map(&:requester_id) + requested_connections.map(&:accepter_id)
  179. end
  180.  
  181. def is_connected?(user)
  182. conn = UserConnection.find(:first, :conditions => [
  183. {:accepter_id => self.id, :requester_id => user.id},
  184. 'or',
  185. {:requester_id => self.id, :accepter_id => user.id}
  186. ])
  187. !conn.nil?
  188. end
  189.  
  190. # a.accept_connection_to u
  191. def accept_connection_to(user)
  192. return false if is_connected?(user) || user.id == self.id
  193.  
  194. uc = UserConnection.new(:accepter_id => self.id, :requester_id => user.id)
  195. conn = uc.save
  196. !conn.nil?
  197. end
  198.  
  199. def search_for_connections(query)
  200. return [] if query.blank?
  201.  
  202. User.find(:all,
  203. :conditions => ["first_name LIKE :query OR last_name LIKE :query OR company like :query", {:query => "%#{query}%"}])
  204. end
  205.  
  206. end
  207.  
  208. __END__
  209.  
  210. class User < ActiveRecord::Base
  211. Fattr :current
  212.  
  213.  
  214. is_paranoid
  215.  
  216. belongs_to :role
  217. has_many :user_role_joins, :dependent => :destroy, :uniq => true
  218. has_many :other_roles, :through => :user_role_joins, :source => :role, :uniq => true
  219. has_one :credit_card, :dependent => :destroy
  220.  
  221. has_many :channels
  222. has_many :orders
  223. has_many :user_connection_user_joins, :dependent => :destroy
  224. has_many :user_connections, :through => :user_connection_user_joins
  225.  
  226. # has_many :dropboxes, :foreign_key => 'owner_id', :order => "name"
  227.  
  228. # has_many :dropbox_user_joins
  229. # has_many :shared_dropboxes, :through => :dropbox_user_joins, :source => :dropbox, :order => "name"
  230.  
  231. accepts_nested_attributes_for :credit_card, :allow_destroy => true, :reject_if => lambda{|hash| hash.values.all?{|value| value.blank?}}
  232.  
  233. def shared_orders_with(user)
  234. [] # eventually this will be real once we can add other users to your order
  235. end
  236.  
  237. def shared_dropboxes_with(user)
  238. # do we need to also add dropboxes where both user_ids are added on a third person's dropbox?
  239. db = []
  240. # db << dropboxes.find(:all, :joins => :dropbox_user_joins, :conditions => {"#{ DropboxUserJoin.table_name }.user_id" => user.id})
  241. # db << user.dropboxes.find(:all, :joins => :dropbox_user_joins, :conditions => {"#{ DropboxUserJoin.table_name }.user_id" => id})
  242. db.flatten
  243. end
  244.  
  245. def add_dropbox(dropbox)
  246. dropbox.add_user(self) unless dropbox.owner_id==id
  247. dropbox
  248. end
  249.  
  250. before_validation_on_create do |user|
  251. user.role ||= Role.for(:user)
  252. end
  253.  
  254. after_create do |user|
  255. user.password ||= User.generate_password
  256. end
  257.  
  258. validates_presence_of :email
  259. validates_presence_of :role
  260. validates_size_of :email, :in => 3..42
  261. validates_uniqueness_of :email, :scope => [:deleted_at]
  262.  
  263. validates_size_of :password, :in => 3..42, :allow_nil => true
  264.  
  265. def User.model_name
  266. self == User ? super : User.model_name
  267. end
  268.  
  269. class Activation < User
  270. attr_accessor :email_confirmation
  271. attr_accessor :password_confirmation
  272. attr_accessor :token
  273.  
  274. validates_presence_of :email
  275. validates_presence_of :password
  276. validates_presence_of :token, :message => 'a valid signup token is required to activate'
  277. validates_confirmation_of :email, :message => 'is not the address used for signup'
  278. validates_confirmation_of :password
  279.  
  280. validates_each :token do |record, attr, value|
  281. if value
  282. token_data = TokenData[value]
  283. email = token_data[:email]
  284. record.errors_add attr, 'invalid token' if record.email != email
  285. #email = Encryption.decrypt(value)
  286. end
  287. end
  288. end
  289.  
  290. def User.activate(*args, &block)
  291. Activation.create(*args, &block)
  292. end
  293.  
  294. def User.authenticate(*args)
  295. options = args.options
  296. args.flatten!
  297.  
  298. email = options.getopt(:email, args.shift).to_s.downcase.strip
  299. password = options.getopt(:password, args.shift).to_s.strip
  300.  
  301. current_controller = ActionController.current
  302. local = options.getopt(:local, current_controller && current_controller.send('local_request?')) rescue false
  303.  
  304. conditions = {:email => email}
  305. user = User.cached(:first, :conditions => conditions)
  306. return nil unless user
  307.  
  308. if(local or (user.password == password))
  309. user
  310. else
  311. false
  312. end
  313. end
  314.  
  315. def User.signup_token_for user_or_email
  316. user = User === user_or_email ? user_or_email : User.new(:email => user_or_email)
  317. @signup_token = TokenData.create!(:data => {:email => user.email})
  318. end
  319.  
  320. def signup_token
  321. @signup_token ||= TokenData.create!(:data => {:email => email})
  322. end
  323.  
  324. def User.admin
  325. User.cached(:first, :conditions => {:email => 'admin@myqval.com'}) or
  326. raise('no admin user!')
  327. end
  328.  
  329. def password
  330. password = self['password']
  331. password.to_s.decrypted if password
  332. end
  333.  
  334. def password= password
  335. self['password'] = password ? password.to_s.encrypted : nil
  336. end
  337.  
  338. def name
  339. if(!first_name.blank? || !last_name.blank?)
  340. [first_name, last_name].join(' ').strip
  341. else
  342. email
  343. end
  344. end
  345.  
  346. def roles
  347. [role, *other_roles]
  348. end
  349.  
  350. def add_roles(*args)
  351. roles_to_add = args.flatten.compact.map{|arg| Role.for(arg)}
  352. roles_to_add.each do |role_to_add|
  353. next if roles.any?{|role| role == role_to_add}
  354. other_roles << role_to_add
  355. end
  356. unless new_record?
  357. save!
  358. reload
  359. end
  360. roles
  361. end
  362. %w( add_role plays! is! can! ).each{|method| alias_method method, 'add_roles'}
  363.  
  364. def has_roles(*args)
  365. return true if roles.any?{|role| role == Role.admin}
  366. roles_to_check = args.flatten.compact.map{|arg| Role.for(arg)}
  367. roles_to_check.all?{|role_to_check| roles.any?{|role| role == role_to_check}}
  368. end
  369. %w( has_roles? has_role has_role? plays plays? is is? can can? ).each{|method| alias_method method, 'has_roles'}
  370.  
  371. def User.generate_password(*args, &block)
  372. String.random(*args, &block)
  373. end
  374.  
  375. def as other
  376. becomes(other)
  377. end
  378.  
  379. def scoping *args, &block
  380. options = args.options
  381. models = args.flatten.compact.uniq
  382.  
  383. user = self
  384.  
  385. scope = {
  386. :find =>
  387. { :conditions => { :user_id => user.id } },
  388.  
  389. :create =>
  390. { :user_id => user.id },
  391. }
  392.  
  393. scope.update options
  394.  
  395. stack = []
  396.  
  397. until models.empty?
  398. model = models.pop
  399. stack.push(
  400. lambda do
  401. model.send(:with_scope, scope, &block)
  402. end
  403. )
  404. block = stack.last unless models.empty?
  405. end
  406.  
  407. stack.last.call
  408. end
  409.  
  410.  
  411.  
  412.  
  413. end
Add Comment
Please, Sign In to add comment