33using Microsoft . AspNetCore . Authentication . OpenIdConnect ;
44using Microsoft . AspNetCore . Authorization ;
55using Microsoft . AspNetCore . Mvc ;
6- using Microsoft . AspNetCore . Mvc . ViewEngines ;
76using SAPSec . Core . Interfaces . Services ;
8- using SAPSec . Core . Model ;
97
108namespace SAPSec . Web . Controllers ;
119
12- [ Route ( "[controller] " ) ]
10+ [ Route ( "auth " ) ]
1311public class AuthController (
1412 IUserService userService ,
1513 ILogger < AuthController > logger ) : Controller
@@ -19,29 +17,14 @@ public class AuthController(
1917
2018 private static class Routes
2119 {
22- public const string SignIn = "sign-in " ;
23- public const string SignOut = "sign-out " ;
20+ public const string SignIn = "signin " ;
21+ public const string SignOut = "signout " ;
2422 public const string SignedOut = "signed-out" ;
25- public const string SignOutCallback = "SignOutCallback" ;
26- public const string SelectOrganisation = "select-organisation" ;
27- }
28-
29- private static class Defaults
30- {
31- public const string ReturnUrl = "/find-a-school" ;
3223 }
3324
3425 private static class LogMessages
3526 {
3627 public const string UserSigningOut = "User {UserId} signing out" ;
37- public const string OrganisationSelected = "User {UserId} selected organisation {OrganisationId}" ;
38- public const string OrganisationSelectionFailed = "Failed to set organisation {OrganisationId} for user {UserId}" ;
39- public const string UserNotFound = "User not found or has no organisations" ;
40- }
41-
42- private static class ErrorMessages
43- {
44- public const string OrganisationIdRequired = "Organisation ID is required" ;
4528 }
4629
4730 [ HttpGet ( Routes . SignIn ) ]
@@ -56,46 +39,7 @@ public IActionResult SignIn(string? returnUrl = null)
5639 return ChallengeWithRedirect ( returnUrl ) ;
5740 }
5841
59- [ HttpGet ( Routes . SelectOrganisation ) ]
60- [ Authorize ]
61- public async Task < IActionResult > SelectOrganisation ( string ? returnUrl = null )
62- {
63- var user = await _userService . GetUserFromClaimsAsync ( User ) ;
64-
65- if ( ! HasValidOrganisations ( user ) )
66- {
67- _logger . LogWarning ( LogMessages . UserNotFound ) ;
68- return RedirectToProblem ( ) ;
69- }
70-
71- ViewBag . ReturnUrl = returnUrl ;
72- return View ( user ) ;
73- }
74-
75- [ HttpPost ( Routes . SelectOrganisation ) ]
76- [ Authorize ]
77- [ ValidateAntiForgeryToken ]
78- public async Task < IActionResult > SelectOrganisationPost ( string organisationId , string ? returnUrl = null )
79- {
80- if ( string . IsNullOrEmpty ( organisationId ) )
81- {
82- return BadRequest ( ErrorMessages . OrganisationIdRequired ) ;
83- }
84-
85- var success = await _userService . SetCurrentOrganisationAsync ( User , organisationId ) ;
86-
87- if ( ! success )
88- {
89- LogOrganisationSelectionFailed ( organisationId ) ;
90- return RedirectToProblem ( ) ;
91- }
92-
93- LogOrganisationSelected ( organisationId ) ;
94- return RedirectToLocal ( returnUrl ) ;
95- }
96-
9742 [ HttpGet ( Routes . SignOut ) ]
98- [ HttpGet ( Routes . SignOutCallback ) ]
9943 [ AllowAnonymous ]
10044 public new async Task < IActionResult > SignOut ( )
10145 {
@@ -127,16 +71,11 @@ private bool IsUserAuthenticated()
12771 return _userService . IsAuthenticated ( User ) ;
12872 }
12973
130- private static bool HasValidOrganisations ( User ? user )
131- {
132- return user ? . Organisations . Any ( ) == true ;
133- }
134-
13574 private IActionResult ChallengeWithRedirect ( string ? returnUrl )
13675 {
13776 var properties = new AuthenticationProperties
13877 {
139- RedirectUri = returnUrl ?? Defaults . ReturnUrl
78+ RedirectUri = returnUrl ?? Constants . Routes . FindASchool
14079 } ;
14180
14281 return Challenge ( properties , OpenIdConnectDefaults . AuthenticationScheme ) ;
@@ -169,11 +108,6 @@ private IActionResult RedirectToHome()
169108 return RedirectToAction ( "Index" , "Home" ) ;
170109 }
171110
172- private IActionResult RedirectToProblem ( )
173- {
174- return RedirectToAction ( "StatusCodeError" , "Error" , new { statusCode = 500 } ) ;
175- }
176-
177111 #endregion
178112
179113 #region Logging Methods
@@ -184,17 +118,5 @@ private void LogUserSigningOut()
184118 _logger . LogInformation ( LogMessages . UserSigningOut , userId ) ;
185119 }
186120
187- private void LogOrganisationSelected ( string organisationId )
188- {
189- var userId = _userService . GetUserId ( User ) ;
190- _logger . LogInformation ( LogMessages . OrganisationSelected , userId , organisationId ) ;
191- }
192-
193- private void LogOrganisationSelectionFailed ( string organisationId )
194- {
195- var userId = _userService . GetUserId ( User ) ;
196- _logger . LogWarning ( LogMessages . OrganisationSelectionFailed , organisationId , userId ) ;
197- }
198-
199121 #endregion
200122}
0 commit comments