summaryrefslogtreecommitdiff
path: root/app/models/user.rb
blob: 7c8ae84eae2f7303e9f299a5f482c82520109170 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
class User < ActiveRecord::Base
	has_and_belongs_to_many :tournaments_played, class_name: "Tournament", foreign_key: "player_id", join_table: "players_tournaments"
	has_and_belongs_to_many :tournaments_hosted, class_name: "Tournament", foreign_key: "host_id", join_table: "hosts_tournaments"
	has_and_belongs_to_many :teams
	has_many :sessions

	apply_simple_captcha

	before_save { self.email = email.downcase }
	before_save { self.user_name = user_name }

	def after_initialize
		self.permissions = 0
	end

	def in_group?(group)
		case group
		when :admin
			return ((groups & 2) != 0)
		when :host
			return true #((groups & 1) != 0)
		when :player
			return true
		when :specator
			return true
		else
			return false
		end
	end

	def join_groups(join=[])
		# FIXME: race condition
		join.each do |group|
			case group
			when :admin
				groups |= 2
			when :host
				groups |= 1
			else
			end
		end
	end

	def leave_groups(leave=[])
		# FIXME: race condition
		leave.each do |group|
			case group
			when :admin
				groups &= ~ 2
			when :host
				groups &= ~ 1
			else
			end
		end
	end

	##
	# VAILD_EMAIL is the regex used to validate a user given email.
	VALID_EMAIL_REG = /\A\S+@\S+\.\S+\z/i

	##
	# VALID_USER_NAME checks to make sure a user's user_name
	# is in the proper format.
	VALID_USER_NAME_REG = /\A[a-zA-Z0-9\-]+\z/

	##
	# The following lines put a user account through a series of
	# validations in order to make sure all of their information
	# is in the proper format.
	#
	#     validates :symbol_to_be_validated
	#
	# - presence: determines whether or not a symbol is filled or not
	# - length: ensures there is a length limit on the symbol
	# - format: checks the format of given information to ensure
	#   validity
	validates(:name, presence: true, length: { maximum: 50 })
	validates(:email, presence: true, format: {with:
				  VALID_EMAIL_REG},
			  uniqueness: { case_sensitive: false })
	validates(:user_name, presence: true, length:{maximum: 50},
			  format: {with: VALID_USER_NAME_REG },
			  uniqueness: {case_sensitive: false })

	##
	# Instead of adding password and password_confirmation
	# attributes, requiring the presence of a password,
	# requiring that pw and pw_com match, and add an authenticate
	# method to compare an encrypted password to the
	# password_digest to authenticate users, I can just add
	# has_secure_password which does all of this for me.
	has_secure_password

	validates :password, length: { minimum: 6 }
end