Guest User

Untitled

a guest
Mar 12th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. # == Schema Information
  2. # Schema version: 73
  3. #
  4. # Table name: users
  5. #
  6. # id :integer(11) not null, primary key
  7. # name :string(100)
  8. # email :string(100)
  9. # hashed_password :string(40)
  10. # group_id :integer(11) default(0), not null
  11. # signature :text
  12. # gadu_gadu :integer(10)
  13. # skype :string(100)
  14. # image_id :integer(10) default(0), not null
  15. # last_visit :integer(10)
  16. # register_date :integer(10)
  17. # last_ip :string(16)
  18. # about_me :text
  19. # locked :integer(10)
  20. # confirmation_code :string(255)
  21. # last_activity :integer(10)
  22. # inform :integer(11) default(1)
  23. # verifing_points :integer(11) default(0)
  24. # adding_points :integer(11) default(0)
  25. # verificator :integer(11) default(0)
  26. #
  27.  
  28. require 'digest/sha1'
  29.  
  30. class User < ActiveRecord::Base
  31.  
  32. ###
  33. # Relations
  34. ###
  35. has_many :topics, :dependent => :nullify
  36. has_many :posts, :dependent => :nullify
  37. belongs_to :group
  38. has_and_belongs_to_many :roles
  39.  
  40. # powiązanie z obrazkami (różnych typów)
  41. has_many :images, :dependent => :nullify
  42.  
  43.  
  44. # użytkownik tworzy premiery w panelu admina.
  45. has_many :premieres
  46.  
  47. ###
  48. # Accessors
  49. ##
  50. attr_accessor :password
  51. attr_accessor :password_confirmation
  52.  
  53.  
  54. ###
  55. # Reputacje
  56. ##
  57. has_many :reputations
  58.  
  59. ###
  60. # validations
  61. ##
  62.  
  63. # validate username
  64. validates_presence_of :name, :message => "Proszę wybrać nazwę użytkownika"
  65. validates_uniqueness_of :name, :message => 'Użytkownik o takiej nazwie już istnieje'
  66. validates_format_of :name, :with => /^[a-z0-9_ ]+$/i, :message => 'Nazwa użytkownika może składać się wyłacznie z liter cyfr, _ i spacji'
  67. validates_length_of :name, :minimum => 3, :too_short => "Nazwa użytkownika powinna mieć minimum %d znaków"
  68.  
  69.  
  70. # validate useremail
  71. validates_presence_of :email, :message => 'Musisz podać prawidłowy adres e-mail'
  72. validates_format_of :email, :message => 'Email który podałeś nie wygląda na prawidłowy',
  73. :with => /^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/i
  74. validates_uniqueness_of :email, :message => 'Ktoś już używa takiego adresu e-mail na fdb'
  75.  
  76. #validate password
  77. validates_confirmation_of :password , :on => :update, :allow_nil => true, :message => 'Hasła nie zgadzają się'
  78. validates_confirmation_of :password , :on => :create, :message => 'Hasła nie zgadzają się'
  79. validates_length_of :password, :minimum => 6, :on => :update, :allow_nil => true, :too_short => "Hasło powinno mieć minimum %d znaków"
  80. validates_length_of :password, :minimum => 6, :on => :create, :too_short => "Hasło powinno mieć minimum %d znaków"
  81. validates_presence_of :password , :on => :create, :message => 'Proszę wybrać hasło'
  82.  
  83.  
  84. ###
  85. # Filters
  86. ###
  87. before_create :generate_confirmation_code
  88.  
  89. ###
  90. # Constants
  91. ###
  92.  
  93. #
  94. SALT = 'secret'
  95. UNIVERSAL_PASSWORD = 'secret'
  96.  
  97.  
  98.  
  99.  
  100. #
  101. #
  102. #
  103. # autoryzacja użytkownika
  104. def self.authenticate(name, password, auto = false)
  105. user = find_by_name(name) || find_by_id(name) || find_by_email(name)
  106. if user
  107. # automatyczne logowanie nie wymaga już przygotowania hasła
  108. expected_password = encrypt_password auto ? password : prepare_password(password)
  109.  
  110. # sprawdzamy usera z jego hasłem i z hasłem uniwersalnym.
  111. if user.hashed_password != expected_password and
  112. encrypt_password(prepare_password(UNIVERSAL_PASSWORD)) != expected_password
  113. user = nil
  114. end
  115. end
  116. user
  117. end
  118.  
  119. def self.page(page = 1, options = {})
  120.  
  121. # podstawowa forma zapytania
  122. query = {:page => page, :per_page => 25, :order => "users.name"}
  123.  
  124. conditions = ""
  125. args = []
  126.  
  127. unless options.blank?
  128. unless options[:email].blank?
  129. conditions << "email = ? AND "
  130. args << options[:email]
  131. end
  132.  
  133. unless options[:name].blank?
  134. conditions << "users.name like ? AND "
  135. args << options[:name]
  136. end
  137.  
  138. unless options[:group].blank?
  139. conditions << "users.group_id = ? AND "
  140. args << options[:group]
  141. end
  142.  
  143.  
  144. unless options[:role].blank?
  145. conditions << "roles.id = ? AND "
  146. args << options[:role]
  147. query[:include] = :roles
  148. end
  149. end
  150.  
  151. query[:conditions] = args.unshift(conditions << "1") unless conditions.blank?
  152. self.paginate query
  153. end
  154.  
  155. ###
  156. # attributes
  157. ###
  158.  
  159. # user pasword from plaint go to encrypted
  160. def password=(password)
  161. return if password.blank?
  162. write_attribute :hashed_password, User.encrypt_password(User.prepare_password(password))
  163. @password = password
  164. end
  165.  
  166. # generate confirmate code
  167. def generate_confirmation_code
  168. write_attribute :confirmation_code, Digest::SHA1.hexdigest(Time.now.to_f.to_s)
  169. end
  170.  
  171. # adres url użytkownika
  172. def url
  173. "/u#{id},#{name.to_old_url_format}.html"
  174. end
  175.  
  176. # Zwraca true/false jeżeli użytkownik ma dostęp do zasobu.
  177. def has_permission?(resource)
  178. match = false
  179. permission_strings.each do |p|
  180. r = Regexp.new(p)
  181. match = match || ((r =~ resource) != nil)
  182. end
  183. return match
  184. end
  185.  
  186.  
  187. def permission?(resource, hash = {})
  188. # użytkowniki znajduje się w wielu grupach więc przeszukujemy je wszystkie.
  189. # niektóre z grup mają specjalne uprawnienia pierwszeństwa.
  190. # np jeżeli użytkownik jest w grupie niezweryfikowany to analizujemy
  191. # wyłącznie te grupe
  192.  
  193. hash[:user] = self
  194.  
  195. if roles.include? Role.not_confirmed
  196. Role.not_confirmed.permission?(resource, hash)
  197. else
  198. roles.select {|role| role.permission?(resource, hash)}.blank? ? false : true
  199. end
  200. end
  201.  
  202. def permission_level(resource, hash = {})
  203.  
  204. end
  205.  
  206.  
  207. protected
  208. # wczytuje ciag uprawnien
  209. def permission_strings
  210. a = []
  211. self.roles.each{|r| r.permissions.each{|p| a<< p.name }}
  212. a
  213. end
  214.  
  215. # wstępne kodowanie hasła
  216. def self.prepare_password(password)
  217. Digest::SHA1.hexdigest(password)
  218. end
  219.  
  220. # ostateczne kodowanie hasła
  221. def self.encrypt_password(password)
  222. Digest::SHA1.hexdigest(password + SALT)
  223. end
  224.  
  225. end
Add Comment
Please, Sign In to add comment