my-services 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/my-services/actions/post.rb +50 -15
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1d4638432fc6b59dc2823bc47d1f6e3ca030ec7bdad38f3c0b70ddc055be184
|
4
|
+
data.tar.gz: 65c2e1cd90b314ae9693c19c498497fa35c5323be3aa5d838f90c4fad24c4550
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b5cb0dbbdd2f8357495d80d7798c93f916f00de0868c8b98cab1f29d649162839935fc02b928c9e5f3abe021fd72119a4979f14737cd881306e7626568eca35
|
7
|
+
data.tar.gz: d86903b3542a25c3fde3c597afe048b17dde413d31e400610b4e25e3acbc5f21ef1a61accd02edcd01aa52d2516fdd39c3a9a7b9d1b51da8ed5c0e964aa18349
|
@@ -2,23 +2,58 @@ module MyServices
|
|
2
2
|
module Actions
|
3
3
|
module Post
|
4
4
|
module ClassMethods
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
def sign_in(email, password)
|
6
|
+
user = User.where(email: email).last
|
7
|
+
|
8
|
+
unless user.present?
|
9
|
+
user = User.where(alternate_email: email).last
|
10
|
+
end
|
11
|
+
|
12
|
+
if user.present? and user.valid_password?(password) and user.role != User::TENANT #and user.role != User::SITE
|
13
|
+
refresh_token = Digest::MD5.hexdigest(Time.now.to_s + user.email)
|
14
|
+
user.update current_sign_in_at: Time.zone.now
|
15
|
+
user.update sign_in_count: (user.sign_in_count + 1)
|
16
|
+
Audit.create(user_id: user.id, action: "login", action_occured_time: Time.zone.now)
|
17
|
+
Rails.logger.request.info(request.headers)
|
18
|
+
login_response(user, refresh_token)
|
19
|
+
else
|
20
|
+
Rails.logger.request.info(request.headers)
|
21
|
+
Rails.logger.auth_failure.info("#{params.inspect}")
|
22
|
+
render json: { success: false, message: "Invalid Email or Password"}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
def login_response(user, refresh_token)
|
26
|
+
client_id = request.headers["REMOTE_ADDR"]
|
27
|
+
$redis.hmset(refresh_token, "user_id", user.id, "client_id", client_id)
|
28
|
+
$redis.expire refresh_token, 43200
|
29
|
+
payload = authentication_payload(user, refresh_token)
|
30
|
+
$redis.sadd(user.email, refresh_token)
|
31
|
+
authentication_token = payload[:authentication_token].split(".").last
|
32
|
+
$redis.hset(authentication_token, "client_id", client_id)
|
33
|
+
render json: payload
|
9
34
|
end
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
35
|
+
def authentication_payload(user, refresh_token)
|
36
|
+
return nil unless user && user.id
|
37
|
+
if user.role == User::TENANT
|
38
|
+
tenant = Tenant.where(user_id: user.id, status: true).first
|
39
|
+
tenant_id = tenant.id
|
40
|
+
name = tenant.first_name + " " + tenant.last_name
|
41
|
+
multiple_tenant_accounts = user.tenants.where(status: true).count > 1 ? true : false
|
42
|
+
elsif user.role == User::SITE
|
43
|
+
name = "Site Admin"
|
44
|
+
else
|
45
|
+
name = "Admin"
|
46
|
+
end
|
47
|
+
site_id = user.site_id if user.site_id.present?
|
48
|
+
{
|
49
|
+
authentication_token: ::AuthToken.encode({ user_id: user.id }),
|
50
|
+
user: { id: user.id, email: user.email, role: user.role, name: name, site_id: site_id,
|
51
|
+
sign_in_count: user.sign_in_count, tenant_id: tenant_id, multiple_tenant_accounts: multiple_tenant_accounts} ,
|
52
|
+
refresh_token: refresh_token,
|
53
|
+
site_id: user.site_id,
|
54
|
+
success: true
|
55
|
+
}
|
20
56
|
end
|
21
|
-
|
22
57
|
end
|
23
58
|
|
24
59
|
def self.included(base)
|