-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathProfileController.cs
146 lines (120 loc) · 5.1 KB
/
ProfileController.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
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using netcore_postgres_oauth_boiler.Models;
using netcore_postgres_oauth_boiler.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace netcore_postgres_oauth_boiler.Controllers
{
[Authorize("Authorized")]
public class ProfileController : Controller
{
private readonly DatabaseContext _context;
public ProfileController(ILogger<AuthController> logger, DatabaseContext context)
{
_context = context;
}
public async Task<IActionResult> Index()
{
var user = await _context.Users.Where(c => Regex.IsMatch(c.Id, HttpContext.Session.GetString("user"))).Include("Credentials").FirstOrDefaultAsync();
if (user.Credentials == null)
{
user.Credentials = new List<Credential>();
}
var blankPassword = BCrypt.Net.BCrypt.Verify("", user.Password);
ViewData["HasPassword"] = user.Password != null && !blankPassword;
ViewData["GoogleLinked"] = user.Credentials.Exists(c => { return c.Provider == AuthProvider.GOOGLE; });
ViewData["GithubLinked"] = user.Credentials.Exists(c => { return c.Provider == AuthProvider.GITHUB; });
ViewData["RedditLinked"] = user.Credentials.Exists(c => { return c.Provider == AuthProvider.REDDIT; });
int amount = user.Credentials.Count;
ViewData["CanUnlinkGoogle"] = !(amount == 1 && (bool)ViewData["GoogleLinked"]);
ViewData["CanUnlinkGithub"] = !(amount == 1 && (bool)ViewData["GithubLinked"]);
ViewData["CanUnlinkReddit"] = !(amount == 1 && (bool)ViewData["RedditLinked"]);
return View();
}
[HttpPost]
public async Task<IActionResult> ChangePassword([FromForm] string currentPassword, [FromForm] string newPassword)
{
// Fetching the user
var user = await _context.Users.Where(c => Regex.IsMatch(c.Id, HttpContext.Session.GetString("user"))).FirstOrDefaultAsync();
// Checking if user exists and verifying password existence
if (user == null || user.Password == null)
{
TempData["error"] = "You can not change your password.";
return View("Index");
}
// Validating password
if (!Validator.validatePassword(newPassword))
{
TempData["error"] = "Password must be between 6 and a 100 characters.";
return View("Index");
}
// Verifying and changing password
try
{
user.Password = BCrypt.Net.BCrypt.ValidateAndReplacePassword(currentPassword, user.Password, newPassword);
}
catch (Exception e)
{
TempData["error"] = "Incorrect old password!";
return View("Index");
}
// Saving changes
await _context.SaveChangesAsync();
// Setting info alert to be shown
TempData["info"] = "You have changed your password!";
// Rendering index
return Redirect("/");
}
[HttpPost]
public async Task<IActionResult> ChangeOAuth(string submit)
{
if (submit == null || submit == "")
{
TempData["error"] = $"No provider supplied.";
return View("Index");
}
// Uppercasing first letter for formatting
submit = submit.First().ToString().ToUpper() + submit.Substring(1);
// Fetching the user
var user = await _context.Users.Where(c => Regex.IsMatch(c.Id, HttpContext.Session.GetString("user"))).Include("Credentials").FirstOrDefaultAsync();
AuthProvider provider;
if (!Enum.TryParse(submit.ToUpper(), out provider))
{
TempData["error"] = $"Invalid provider.";
return View("Index");
}
Credential c = user.Credentials.FirstOrDefault(c => { return c.Provider == provider; });
if (c != null)
{
// Unlinking if possible
if (user.Password != null || (user.Credentials.Count > 1))
{
user.Credentials.Remove(c);
}
else
{
// Should not be reachable.
TempData["error"] = $"You cannot unlink {submit}.";
return View("Index");
}
}
else
{
return Redirect($"/OAuth/{submit}");
}
// Saving changes
await _context.SaveChangesAsync();
// Setting info alert to be shown
TempData["info"] = $"You have unlinked {submit}!";
// Rendering index
return Redirect("/");
}
}
}