Browse code

Ukladanie jid do databazy pri prihlaseni Prepisane modely do menej-statickeho pristupu

Cinan Rakosnik authored on 12/03/2013 at 20:02:10
Showing 4 changed files
... ...
@@ -4,7 +4,7 @@ class ApplicationController < ActionController::Base
4 4
 
5 5
 	def require_login
6 6
 		if authenticated?
7
-			create_new_authentification()
7
+			create_new_authentication()
8 8
 
9 9
             unless controller_name == 'chat'
10 10
                 redirect_to chat_template_path
... ...
@@ -19,16 +19,32 @@ class ApplicationController < ActionController::Base
19 19
 	protected
20 20
 
21 21
 	def authenticated?
22
-		!! Token.authenticate(session)
22
+        @token = Token.authenticate(session)
23 23
 	end
24 24
 
25
-	def create_new_authentification
26
-		Token.remove_old_session(session[:token])
25
+	def create_new_authentication(jid = nil)
26
+        if @token
27
+            user_id = @token.user_id
28
+            @token.delete
29
+        else
30
+            user_id = jid.nil? ? nil : create_new_user(jid)
31
+        end
27 32
 
28
-		session[:token] = Token.generate_token()
29
-		session[:created_at] = Time.now
30
-		session[:ip] = request.remote_ip
33
+        @token = Token.new
31 34
 
32
-		Token.save_session(session)
33
-	end
35
+        save_session(user_id)
36
+    end
37
+
38
+    def save_session(user_id)
39
+        session[:token] = Token.generate_token()
40
+        session[:created_at] = Time.now
41
+        session[:ip] = request.remote_ip
42
+
43
+        @token.save_session(session, user_id)
44
+    end
45
+
46
+    def create_new_user(jid)
47
+        user = User.new(jid: jid)
48
+        user.id if user.save
49
+    end
34 50
 end
... ...
@@ -13,7 +13,7 @@ class SessionsController < ApplicationController
13 13
 			return render 'new'
14 14
 		end
15 15
 
16
-        create_new_authentification()
16
+        create_new_authentication(params[:jid].downcase)
17 17
         redirect_to chat_template_path, flash: {notice: I18n.t('login.success')}
18 18
 	end
19 19
 
... ...
@@ -9,9 +9,12 @@ class Token
9 9
 	field :token, type: String
10 10
 	field :date_expiring, type: DateTime
11 11
 	field :ip, type: Integer
12
+    field :user_id, type: Moped::BSON::ObjectId
12 13
 
14
+    ##
15
+    # @return [Token]
13 16
 	def self.authenticate(session)
14
-		if session[:created_at] == nil
17
+		if session[:created_at].nil?
15 18
 			return false
16 19
 		end
17 20
 
... ...
@@ -24,18 +27,19 @@ class Token
24 24
 		matched_token
25 25
 	end
26 26
 
27
-	def self.save_session(session)
28
-		date_expiring = session[:created_at] + Rails.application.config.max_lifetime
27
+	def save_session(session, user)
28
+        self.token         = session[:token]
29
+        self.date_expiring = session[:created_at] + Rails.application.config.max_lifetime
30
+        self.created_at    = session[:created_at]
31
+        self.ip			   = IPAddr.new(session[:ip]).to_i
32
+        self.user_id       = user
29 33
 
30
-		self.create!(:token 		=> session[:token],
31
-					 :date_expiring	=> date_expiring,
32
-					 :created_at	=> session[:created_at],
33
-					 :ip			=> IPAddr.new(session[:ip]).to_i)
34
-	end
34
+        save
35
+    end
35 36
 
36
-	def self.remove_old_session(token)
37
-		self.delete_all(token: token)
38
-	end
37
+	#def self.remove_old_session(token)
38
+	#	self.delete_all(token: token)
39
+	#end
39 40
 
40 41
 	private
41 42
 
... ...
@@ -1,5 +1,6 @@
1 1
 class User
2 2
 	include Mongoid::Document
3 3
 
4
+    field :id, type: Moped::BSON::ObjectId
4 5
 	field :jid, type: String
5 6
 end
6 7
\ No newline at end of file