Repository navigation
Expand file tree
/
Copy pathExternalAuthFacebookController.cs
More file actions
153 lines (129 loc) · 5.28 KB
/
Copy pathExternalAuthFacebookController.cs
File metadata and controls
153 lines (129 loc) · 5.28 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
using System.Web.Mvc;
using SmartStore.ComponentModel;
using SmartStore.Core.Domain.Customers;
using SmartStore.FacebookAuth.Core;
using SmartStore.FacebookAuth.Models;
using SmartStore.Services;
using SmartStore.Services.Authentication.External;
using SmartStore.Services.Security;
using SmartStore.Web.Framework;
using SmartStore.Web.Framework.Controllers;
using SmartStore.Web.Framework.Security;
using SmartStore.Web.Framework.Settings;
namespace SmartStore.FacebookAuth.Controllers
{
//[UnitOfWork]
public class ExternalAuthFacebookController : PluginControllerBase
{
private readonly IOAuthProviderFacebookAuthorizer _oAuthProviderFacebookAuthorizer;
private readonly IOpenAuthenticationService _openAuthenticationService;
private readonly ExternalAuthenticationSettings _externalAuthenticationSettings;
private readonly ICommonServices _services;
public ExternalAuthFacebookController(
IOAuthProviderFacebookAuthorizer oAuthProviderFacebookAuthorizer,
IOpenAuthenticationService openAuthenticationService,
ExternalAuthenticationSettings externalAuthenticationSettings,
ICommonServices services)
{
_oAuthProviderFacebookAuthorizer = oAuthProviderFacebookAuthorizer;
_openAuthenticationService = openAuthenticationService;
_externalAuthenticationSettings = externalAuthenticationSettings;
_services = services;
}
private bool HasPermission(bool notify = true)
{
var hasPermission = _services.Permissions.Authorize(StandardPermissionProvider.ManageExternalAuthenticationMethods);
if (notify && !hasPermission)
NotifyError(_services.Localization.GetResource("Admin.AccessDenied.Description"));
return hasPermission;
}
[LoadSetting, AdminAuthorize, ChildActionOnly]
public ActionResult Configure(FacebookExternalAuthSettings settings)
{
if (!HasPermission(false))
return AccessDeniedPartialView();
var model = new ConfigurationModel();
MiniMapper.Map(settings, model);
var host = _services.StoreContext.CurrentStore.GetHost(true);
model.RedirectUrl = $"{host}Plugins/SmartStore.FacebookAuth/logincallback/";
return View(model);
}
[SaveSetting, HttpPost, AdminAuthorize, ChildActionOnly]
public ActionResult Configure(FacebookExternalAuthSettings settings, ConfigurationModel model)
{
if (!HasPermission(false))
return Configure(settings);
if (!ModelState.IsValid)
return Configure(settings);
MiniMapper.Map(model, settings);
settings.ClientKeyIdentifier = model.ClientKeyIdentifier.TrimSafe();
settings.ClientSecret = model.ClientSecret.TrimSafe();
NotifySuccess(_services.Localization.GetResource("Admin.Common.DataSuccessfullySaved"));
return RedirectToConfiguration(FacebookExternalAuthMethod.SystemName, true);
}
[ChildActionOnly]
public ActionResult PublicInfo()
{
var settings = _services.Settings.LoadSetting<FacebookExternalAuthSettings>(_services.StoreContext.CurrentStore.Id);
if (settings.ClientKeyIdentifier.HasValue() && settings.ClientSecret.HasValue())
return View();
else
return new EmptyResult();
}
public ActionResult Login(string returnUrl)
{
return LoginInternal(returnUrl, false);
}
public ActionResult LoginCallback(string returnUrl)
{
return LoginInternal(returnUrl, true);
}
[NonAction]
private ActionResult LoginInternal(string returnUrl, bool verifyResponse)
{
var processor = _openAuthenticationService.LoadExternalAuthenticationMethodBySystemName(FacebookExternalAuthMethod.SystemName, _services.StoreContext.CurrentStore.Id);
if (processor == null || !processor.IsMethodActive(_externalAuthenticationSettings))
{
NotifyError(T("Plugins.CannotLoadModule", T("Plugins.FriendlyName.SmartStore.FacebookAuth")));
return new RedirectResult(Url.LogOn(returnUrl));
}
var viewModel = new LoginModel();
TryUpdateModel(viewModel);
var result = _oAuthProviderFacebookAuthorizer.Authorize(returnUrl, verifyResponse);
switch (result.AuthenticationStatus)
{
case OpenAuthenticationStatus.Error:
{
if (!result.Success)
{
result.Errors.Each(x => NotifyError(x));
}
return new RedirectResult(Url.LogOn(returnUrl));
}
case OpenAuthenticationStatus.AssociateOnLogon:
{
return new RedirectResult(Url.LogOn(returnUrl));
}
case OpenAuthenticationStatus.AutoRegisteredEmailValidation:
{
return RedirectToRoute("RegisterResult", new { resultId = (int)UserRegistrationType.EmailValidation, returnUrl });
}
case OpenAuthenticationStatus.AutoRegisteredAdminApproval:
{
return RedirectToRoute("RegisterResult", new { resultId = (int)UserRegistrationType.AdminApproval, returnUrl });
}
case OpenAuthenticationStatus.AutoRegisteredStandard:
{
return RedirectToRoute("RegisterResult", new { resultId = (int)UserRegistrationType.Standard, returnUrl });
}
default:
break;
}
if (result.Result != null)
return result.Result;
return HttpContext.Request.IsAuthenticated ?
RedirectToReferrer(returnUrl, "~/") :
new RedirectResult(Url.LogOn(returnUrl));
}
}
}