/
HomeController.cs
227 lines (215 loc) · 8.53 KB
/
HomeController.cs
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
220
221
222
223
224
225
226
227
using Autofac.Extras.NLog;
using Microsoft.AspNet.Identity;
using Microsoft.Owin.Security;
using Promact.Core.Repository.ExternalLoginRepository;
using Promact.Erp.DomainModel.Models;
using Promact.Erp.Util.EnvironmentVariableRepository;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Promact.Erp.Util.StringConstants;
using System.Net.Http;
using Promact.Erp.Util.HashingMd5;
using System;
using Promact.Erp.DomainModel.ApplicationClass;
namespace Promact.Erp.Core.Controllers
{
public class HomeController : MVCBaseController
{
#region Private Variables
private readonly ApplicationSignInManager _signInManager;
private readonly ApplicationUserManager _userManager;
private readonly ILogger _logger;
private readonly IOAuthLoginRepository _oAuthLoginRepository;
private readonly IEnvironmentVariableRepository _envVariableRepository;
private readonly IMd5Service _md5Service;
#endregion
#region Constructor
public HomeController(ApplicationUserManager userManager, IStringConstantRepository stringConstant,
ApplicationSignInManager signInManager, ILogger logger, IOAuthLoginRepository oAuthLoginRepository,
IEnvironmentVariableRepository envVariableRepository, IMd5Service md5Service) : base(stringConstant)
{
_userManager = userManager;
_signInManager = signInManager;
_logger = logger;
_oAuthLoginRepository = oAuthLoginRepository;
_envVariableRepository = envVariableRepository;
_md5Service = md5Service;
}
#endregion
#region Public Methods
/**
* @api {get} Home/Index
* @apiVersion 1.0.0
* @apiName Index
* @apiGroup Index
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "Description":"Open the first/login page of the application"
* }
*/
public ActionResult Index()
{
if (User.Identity.IsAuthenticated)
{
return RedirectToAction(_stringConstantRepository.AfterLogIn, _stringConstantRepository.Home);
}
return View();
}
/**
* @api {get} Home/AfterLogIn
* @apiVersion 1.0.0
* @apiName AfterLogIn
* @apiGroup AfterLogIn
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "Description":"After Login from OAuth server Page will be redirected to this page and will open a view of application"
* }
*/
[Authorize]
public async Task<ActionResult> AfterLogIn()
{
string userId = GetUserId(User.Identity);
//for check login user is already added in slack
ViewBag.userEmail = await _oAuthLoginRepository.CheckUserSlackInformation(userId);
//this for get login user email address and encrypt hash code.
ApplicationUser user = await _userManager.FindByIdAsync(userId);
EmailHashCodeAC emailHaseCodeAC = new EmailHashCodeAC(_md5Service.GetMD5HashData(user.Email.ToLower()));
ViewBag.emailHashCode = emailHaseCodeAC;
return View();
}
/**
* @api {get} Home/SlackAuthorize
* @apiVersion 1.0.0
* @apiName SlackAuthorize
* @apiGroup SlackAuthorize
* @apiParam {string} Name message
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "Description":"After Slack OAuth Authorization, user is redirected here with the status of Authorization message."
* }
*/
public ActionResult SlackAuthorize(string message)
{
ViewBag.Message = message;
return View();
}
/**
* @api {get} Home/ExtrenalLogin
* @apiVersion 1.0.0
* @apiName ExtrenalLogin
* @apiGroup ExtrenalLogin
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "Description":"Will redirect to OAuth server for external login"
* }
*/
public ActionResult ExtrenalLogin()
{
if (User.Identity.IsAuthenticated)
{
return RedirectToAction(_stringConstantRepository.AfterLogIn, _stringConstantRepository.Home);
}
//BaseUrl of OAuth and clientId of App to be set
string url = string.Format("{0}?clientId={1}", _stringConstantRepository.OAuthUrl, _envVariableRepository.PromactOAuthClientId);
//make call to the OAuth Server
return Redirect(url);
}
/**
* @api {get} Home/ExtrenalLoginCallBack
* @apiVersion 1.0.0
* @apiName ExtrenalLoginCallBack
* @apiGroup ExtrenalLoginCallBack
* @apiParam {string} Name accessToken
* @apiParam {string} Name email
* @apiParam {string} Name slackUserName
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "Description":"Redirect to a view page of application and user will be added from external OAuth to our application"
* }
*/
public async Task<ActionResult> ExtrenalLoginCallBack(string accessToken, string email, string slackUserId, string userId)
{
ApplicationUser user = _userManager.FindByEmail(email);
if (user != null)
{
await _signInManager.SignInAsync(user, false, false);
return RedirectToAction(_stringConstantRepository.AfterLogIn, _stringConstantRepository.Home);
}
if (User.Identity.IsAuthenticated)
{
return RedirectToAction(_stringConstantRepository.AfterLogIn, _stringConstantRepository.Home);
}
if (user == null)
{
user = await _oAuthLoginRepository.AddNewUserFromExternalLoginAsync(email, accessToken, userId);
if (user != null)
{
//Signing user with username or email only
await _signInManager.SignInAsync(user, false, false);
return RedirectToAction(_stringConstantRepository.AfterLogIn, _stringConstantRepository.Home);
}
return RedirectToAction(_stringConstantRepository.SlackAuthorize, _stringConstantRepository.Home, new { message = _stringConstantRepository.UserCouldNotBeAdded });
}
return View();
}
/**
* @api {get} Home/LogOff
* @apiVersion 1.0.0
* @apiName LogOff
* @apiGroup LogOff
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "Description":"SignOut from our application"
* }
*/
public ActionResult LogOff()
{
//Set the cookie to expire
Request.GetOwinContext().Authentication.SignOut("Cookies");
return RedirectToAction(_stringConstantRepository.Index, _stringConstantRepository.Home);
}
private IAuthenticationManager AuthenticationManager
{
get
{
return HttpContext.GetOwinContext().Authentication;
}
}
/**
* @api {get} Home/SlackOAuthAuthorization
* @apiVersion 1.0.0
* @apiName SlackOAuthAuthorization
* @apiGroup SlackOAuthAuthorization
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "Description":"Add to slack button will redirect here and it will open a Slack OAuth Authorization Page for our app"
* }
*/
public ActionResult SlackOAuthAuthorization()
{
try
{
if (User.Identity.IsAuthenticated)
{
return Redirect(_stringConstantRepository.LeaveManagementAuthorizationUrl + _stringConstantRepository.OAuthAuthorizationScopeAndClientId + _envVariableRepository.SlackOAuthClientId);
}
return RedirectToAction(_stringConstantRepository.Index, _stringConstantRepository.Home);
}
catch (HttpRequestException ex)
{
var errorMessage = string.Format("{0}. Error -> {1}", _stringConstantRepository.LoggerErrorMessageHomeControllerSlackOAuthAuthorization, ex.ToString());
_logger.Error(errorMessage, ex);
throw;
}
}
#endregion
}
}