Changeset 1171
- Timestamp:
- 02/28/08 18:08:58 (10 months ago)
- Files:
-
- trunk/app/models/user.rb (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/app/models/user.rb
r1138 r1171 2 2 class User < ActiveRecord::Base 3 3 has_many :reviews, :dependent => :destroy 4 4 5 5 has_many :documents, :dependent => :destroy, :order => "updated_at DESC", :foreign_key => "author_id" do 6 6 def find_by_tag(name, options = Hash.new) 7 7 tag = Tag.find_by_name(name) 8 8 documents = find(:all).delete_if { |d| !d.tags.include?(tag) } 9 9 10 10 if options[:offset] and options[:limit] 11 11 documents.slice!(options[:offset], options[:limit]) 12 12 end 13 13 14 14 documents 15 15 end 16 16 17 17 def find_by_keywords(keywords) 18 18 find(:all).delete_if { |d| !d.match?(keywords) } 19 19 end 20 20 end 21 21 22 22 has_many :subscriptions, :dependent => :destroy, :order => "created_at DESC", :as => "subscriber" 23 23 24 24 has_many :podcasts, :dependent => :destroy, :foreign_key => "author_id" 25 25 26 26 has_and_belongs_to_many :groups 27 27 has_many :manageable_groups, :class_name => "Group", :foreign_key => "owner_id" … … 33 33 validates_presence_of :password, :message => "Un mot de passe est requis", :if => Proc.new { |u| u.openid_url.nil? } 34 34 validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/, :message => "Un email valide pour vous contacter est requis" 35 35 36 36 def tags 37 37 subscriptions_tags = find_subscriptions.collect{ |s| s.document.tags }.flatten … … 39 39 (subscriptions_tags + documents_tags).uniq 40 40 end 41 41 42 42 def find_subscriptions(options = Hash.new) 43 43 group_subscriptions = groups.collect{ |group| group.subscriptions }.flatten … … 46 46 sorted_attribute = "created_at" 47 47 all_subscriptions = all_subscriptions.sort_by { |s| s[sorted_attribute] }.reverse 48 48 49 49 if options[:tag] 50 50 options[:tags] = Tag.parse(options[:tag]) 51 51 end 52 52 53 53 if options[:tags] 54 54 tags = Tag.parse(options[:tags]) 55 55 all_subscriptions = all_subscriptions.delete_if { |s| ! s.document.match_tags?(tags) } 56 56 end 57 57 58 58 if options[:keywords] 59 all_subscriptions = all_subscriptions.delete_if do |s| 59 all_subscriptions = all_subscriptions.delete_if do |s| 60 60 !s.document.match?(options[:keywords]) 61 61 end … … 65 65 return all_subscriptions.slice(options[:offset], options[:limit]) 66 66 end 67 67 68 68 all_subscriptions 69 69 end 70 70 71 71 def find_documents(options = Hash.new) 72 72 documents = self.documents.find(:all) … … 75 75 options[:tags] = Tag.parse(options[:tag]) 76 76 end 77 77 78 78 if options[:tags] 79 79 tags = Tag.parse(options[:tags]) 80 documents = documents.delete_if do |document| 80 documents = documents.delete_if do |document| 81 81 !document.match_tags?(tags) 82 82 end … … 84 84 85 85 if options[:keywords] 86 documents = documents.delete_if do |document| 86 documents = documents.delete_if do |document| 87 87 !document.match?(options[:keywords]) 88 88 end 89 89 end 90 90 91 91 if options[:offset] and options[:limit] 92 92 documents.slice!(options[:offset], options[:limit]) 93 93 end 94 94 95 95 documents 96 96 end 97 98 97 98 99 99 def find_subscription(id) 100 100 subscription = self.subscriptions.find_by_id(id) 101 for group in groups 102 subscription = group.subscriptions.find_by_id(id) 103 break unless subscription.nil? 104 end 101 unless subscription 102 for group in groups 103 subscription = group.subscriptions.find_by_id(id) 104 break unless subscription.nil? 105 end 106 end 105 107 subscription 106 108 end 107 109 108 110 def self.digest_password(clear_password) 109 111 Digest::SHA256.hexdigest(clear_password) 110 112 end 111 113 112 114 def password=(password) 113 115 write_attribute(:password, User.digest_password(password)) unless password.empty? 114 116 end 115 117 116 118 def self.authenticate(username, clear_password) 117 119 user = User.find_by_username(username, :conditions => ["confirmed = ?", true]) 118 120 119 121 if user.blank? 120 122 logger.debug("unknown or unconfirmed user : #{username}") 121 123 return nil 122 124 end 123 125 124 126 if User.digest_password(clear_password) != user.password 125 127 logger.debug("wrong password for : #{username}") 126 128 return nil 127 129 end 128 130 129 131 logger.info("user logged : #{username}") 130 132 user 131 133 end 132 134 133 135 def match_name?(input) 134 136 (self.name.downcase.include?(input) or (not self.username.nil? and self.username.downcase.include?(input))) 135 137 end 136 138 137 139 def ==(other) 138 140 id == other.id 139 141 end 140 142 141 143 def hashcode 142 144 Digest::SHA256.hexdigest(id.to_s + email) 143 145 end 144 146 145 147 def confirmed? 146 148 confirmed 147 149 end 148 150 149 151 def after_save 150 152 User.destroy_all(["confirmed = ? AND created_at <= ?", false, Time.new - 2592000])
