Browse code

Provizorne ukladanie udajov o pouzivatelovi do databazy

Zatial nie je doriesene enkryptovanie hesiel v databaze.

Cinan Rakosnik authored on 17/03/2013 at 14:33:56
Showing 3 changed files
... ...
@@ -22,17 +22,17 @@ class ApplicationController < ActionController::Base
22 22
         @token = Token.authenticate(session)
23 23
 	end
24 24
 
25
-	def create_new_authentication(jid = nil)
25
+	def create_new_authentication(user_credentials = nil)
26 26
         if @token
27 27
             user_id = @token.user_id
28 28
             @token.delete
29 29
         else
30
-            user_id = jid.nil? ? nil : create_new_user(jid)
30
+            user_id = !user_credentials ? nil : create_new_user(user_credentials)
31 31
         end
32 32
 
33 33
         @token = Token.new
34 34
 
35
-        save_session(user_id)
35
+        save_session(user_id) if not user_id
36 36
     end
37 37
 
38 38
     def save_session(user_id)
... ...
@@ -43,8 +43,13 @@ class ApplicationController < ActionController::Base
43 43
         @token.save_session(session, user_id)
44 44
     end
45 45
 
46
-    def create_new_user(jid)
47
-        user = User.new(jid: jid)
48
-        user.id if user.save
46
+    def create_new_user(user_credentials)
47
+        jid  = user_credentials[:jid]
48
+        pass = user_credentials[:password]
49
+
50
+        user = User.existing_jid(jid) || User.create_jid(jid)
51
+        user.update_pass(jid, pass)
52
+
53
+        user.id
49 54
     end
50 55
 end
... ...
@@ -13,7 +13,11 @@ class SessionsController < ApplicationController
13 13
 			return render 'new'
14 14
 		end
15 15
 
16
-        create_new_authentication(params[:jid].downcase)
16
+        create_new_authentication({
17
+            jid: params[:jid].downcase,
18
+            password: params[:password]
19
+        })
20
+
17 21
         redirect_to chat_path, flash: {notice: I18n.t('login.success')}
18 22
 	end
19 23
 
... ...
@@ -1,6 +1,29 @@
1 1
 class User
2 2
 	include Mongoid::Document
3 3
 
4
-    field :id, type: Moped::BSON::ObjectId
5
-	field :jid, type: String
4
+	field :jids, type: Array
5
+
6
+    def update_pass(jid, pass)
7
+        account_credentials = jids.detect do |f|
8
+            f[:jid] == jid || f["jid"] == jid
9
+        end
10
+
11
+        account_credentials[:pass] = pass
12
+        save
13
+    end
14
+
15
+    def self.existing_jid(jid)
16
+        where("jids.jid" => jid).only(:jids).first
17
+    end
18
+
19
+    def self.create_jid(jid)
20
+        new_user = new(jids: [ {jid: jid} ])
21
+        new_user.save
22
+        new_user
23
+    end
24
+
25
+    def add_account(another_jid, password)
26
+        jids << {jid: another_jid, pass: password}
27
+        save
28
+    end
6 29
 end
7 30
\ No newline at end of file