AuthenticateController.cs
9.71 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using log4net;
using AIAHTML5.API.Constants;
using AIAHTML5.API.Models;
using System.Collections;
namespace AIAHTML5.API.Controllers
{
public class AuthenticateController : ApiController
{
// GET api/authenticate
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/authenticate/5
public string Get(int id)
{
return "value";
}
// POST api/authenticate
public HttpResponseMessage Post([FromBody]JObject credentials)
{
ILog logger = log4net.LogManager.GetLogger((System.Reflection.MethodBase.GetCurrentMethod().DeclaringType));
logger.Debug("inside POST");
dynamic authenticationRepsonse;
//01. check user is authenticated or not by login credential macth
//bool isUserAuthenticated = AIAHTML5.API.Models.Users.IsUserAuthenticated(credentials);
//Above code commented to reduce dbhitting for same result set
User userInfo = AIAHTML5.API.Models.Users.getUserDetails(credentials);
//check is user authenticated
bool isUserAuthenticated = AIAHTML5.API.Models.Users.IsUserAuthenticated(credentials, userInfo);
if (isUserAuthenticated)
{
//01. Get User details
//userInfo = AIAHTML5.API.Models.Users.getUserDetails(credentials);
//02. assigning isCorrectPassword to true 'required for internal processing'
userInfo.IsCorrectPassword = true;
//03.insert Log login details
// Below statement executing irrespective of the fact user license inactive
//AIAHTML5.API.Models.Users.insertLoginDetails(userInfo.Id);
//04.delete past wrong login attempts of user
int wrongAttemptDeteledCount = AIAHTML5.API.Models.Users.deletePastWrongAttempts(userInfo.Id);
if (wrongAttemptDeteledCount <= 0)
{
logger.Fatal("Unable to delete past wrong login attempts for userId= " + userInfo.Id);
}
//05. Now get the module list- for ADMIN (superadmin/ general admin) by default all module loads
if (userInfo.UserType == AIAHTML5.API.Models.User.SUPER_ADMIN || userInfo.UserType == AIAHTML5.API.Models.User.GENERAL_ADMIN)
{
userInfo.Modules = AIAHTML5.API.Models.Users.getAllModulesList();
//Insert user login detail
AIAHTML5.API.Models.Users.insertLoginDetails(userInfo.Id);
}
else
{
//CORRECT CODE
//05.1 For normal user need to get the license details, get the license id for aUTHENTICATED USER
int licenseId, editionId;
AIAHTML5.API.Models.Users.getLicenseIdForThisUser(userInfo.Id, out licenseId, out editionId);
userInfo.LicenseId = licenseId;
userInfo.EditionId = editionId;
//05.2 Check user is active or not
//05.3 get license/ licenseSubscription details
userInfo.LicenseInfo = AIAHTML5.API.Models.Users.getLicenseDetails(userInfo.LicenseId);
//05.4
userInfo.LicenseSubscriptions = AIAHTML5.API.Models.Users.getLicenseSubscriptionDetails(userInfo.LicenseId);
//05.5 check the License expiration irespective of either user is active or not because on AIA
//we shows the License expiration message for inactive users too
string expirationDate = null;
bool isLicenseExpired = AIAHTML5.API.Models.Users.checkIfLicenseExpired(userInfo.LicenseSubscriptions, out expirationDate);
// send message to the UI for license expiration
//05.6 Check for subscription Expiration [Promoted for case if license inactive along with subscription expired]
if (isLicenseExpired)
{
userInfo.IsSubscriptionExpired = isLicenseExpired;
userInfo.SubscriptionExpirationDate = expirationDate;
}
else
{
//05.6.1
if (userInfo.LicenseInfo.IsActive)
{
if (!userInfo.LicenseInfo.IsTermAccepted)
{
ArrayList termsList = AIAHTML5.API.Models.Users.getTermsOfServiceText();
foreach (Hashtable item in termsList)
{
userInfo.TermsOfServiceTitle = item["title"].ToString();
userInfo.TermsOfServiceText = item["content"].ToString();
}
}
else
{
userInfo.Modules = AIAHTML5.API.Models.Users.getModuleListByLicenseId(userInfo.LicenseId);
//Insert user login detail
AIAHTML5.API.Models.Users.insertLoginDetails(userInfo.Id);
}
}
else
{
//05.6.1.1
// return message of license inactive
// property value assigned. Separate return statement not required
}
}
}
authenticationRepsonse = JsonConvert.SerializeObject(userInfo);
}
else
{
bool isCorrectLoginId, isCorrectPassword;
AIAHTML5.API.Models.Users.isCredentialCorrect(credentials, out isCorrectLoginId, out isCorrectPassword);
if (!isCorrectLoginId)
{
// send message back to th UI that login id is incorrect
authenticationRepsonse = AIAConstants.USER_NOT_FOUND;
}
else
{
//getting userDetails
userInfo = AIAHTML5.API.Models.Users.getUserDetails(credentials);
if (!isCorrectPassword)
{
// send message back to th UI that password is incorrect
userInfo.IsCorrectPassword = false;
//get wrong attempt count of user
userInfo.IncorrectLoginAttemptCount = AIAHTML5.API.Models.Users.checkNoOfWrongAttempts(userInfo.Id) +1;
userInfo.LoginFailureCauseId = ErrorHelper.E_PASSWORD_NOT_MATCH;
//01. insert wrong attempt in dtabase
int updateCount = AIAHTML5.API.Models.Users.saveWrongAttemptofUser(userInfo.Id);
if (updateCount < 0)
{
//Put the log in log file
logger.Fatal("Unable to Update past wrong login attempts for userId= " + userInfo.Id);
}
else
{
if (userInfo.IncorrectLoginAttemptCount > 4)
{
userInfo.IsBlocked = true;
userInfo.LoginFailureCauseId = ErrorHelper.E_USER_ID_BLOCKED_24_HRS;
}
}
}
// unreachable code detected as license is null
//if (userInfo.License != null && !string.IsNullOrEmpty(userInfo.License.AccountNumber))
//{
// int result = AIAHTML5.API.Models.Users.insertUserLoginLog(userInfo.License.AccountNumber, userInfo.LoginFailureCauseId, null, userInfo.EditionId.ToString(), null);
// if (result < 0)
// logger.Fatal("Unable to insert wrong attempt detail in UserLoginLog table for accountNumber= " + userInfo.License.AccountNumber);
//}
authenticationRepsonse = JsonConvert.SerializeObject(userInfo);
}
}
//if (Convert.ToString(authenticationRepsonse) != AIAConstants.USER_NOT_FOUND && Convert.ToString(authenticationRepsonse) != AIAConstants.ERROR_IN_FECTHING_DETAILS && Convert.ToString(authenticationRepsonse)!= AIAConstants.SQL_CONNECTION_ERROR)
//{
// //string userDetails = Newtonsoft.Json.JsonConvert.SerializeObject(authenticationRepsonse);
// return new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(authenticationRepsonse) };
//}
//else
//{
return new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(authenticationRepsonse) };
//}
}
// PUT api/authenticate/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/authenticate/5
public void Delete(int id)
{
}
}
}