public JsonResult LoadAccounts(string email) { try { int userId = getUserId(email); var response = new Dictionary <string, List <Object> >(); using (var db = new PassManagerContext()) { // group by tag foreach (var acc in db.Accounts) { if (acc.userID == userId) { if (response.ContainsKey(acc.tag)) { response[acc.tag].Add(new { websiteUrl = acc.websiteUrl }); } else { response[acc.tag] = new List <Object>(); response[acc.tag].Add(new { websiteUrl = acc.websiteUrl }); } } } } return(Json(response)); } catch (Exception e) { return(Json(new { reason = "An error has ocuured" })); } }
public JsonResult DeleteUser(string email) { try { using (var db = new PassManagerContext()) { User user = getUser(email); // remove user accounts var accounts = db.Accounts.Where(a => a.userID == user.ID); foreach (Account acc in accounts) { db.Accounts.Remove(acc); } // remove user db.Users.Remove(user); db.SaveChanges(); return(Json(new { succeeded = true })); } } catch (Exception e) { return(Json(new { succeeded = false, reason = "An error occured" })); } }
public JsonResult updateEmail(string email, string newEmail) { if (CheckEmailExists(newEmail)) { return(Json(new { succeeded = false, reason = "Email already exists." })); } try { using (var db = new PassManagerContext()) { db.Users .Single(u => u.email.ToLower() == email.ToLower()) .email = newEmail; db.SaveChanges(); return(Json(new { succeeded = true, token = TokenController.IssueToken(newEmail) })); } } catch (Exception e) { return(Json(new { succeeded = false, reason = "An error occured" })); } }
public JsonResult LoadCreds(string websiteUrl, string email) { try { int userId = getUserId(email); using (var db = new PassManagerContext()) { Account account = db.Accounts .Single(a => a.userID == userId && a.websiteUrl == websiteUrl); if (account != null) { return(Json(new { creds = account })); } else { return(Json(new { reason = "Not Found" })); } } } catch (Exception e) { return(Json(new { reason = "An error has ocuured" })); } }
public JsonResult AddAccount(string accountJson, string email) { try { int userId = getUserId(email); Account account = JsonConvert.DeserializeObject <Account>(accountJson); using (var db = new PassManagerContext()) { // check if account is already present if (db.Accounts .Where(a => a.websiteUrl == account.websiteUrl && a.userID == userId) .ToList() .Any()) { return(Json(new { succeeded = false, reason = "Account already exists." })); } account.userID = userId; db.Accounts.Add(account); db.SaveChanges(); return(Json(new { succeeded = true })); } } catch (Exception e) { return(Json(new { succeeded = false, reason = "An error occurred." })); } }
public JsonResult Delete(string websiteUrl, string email) { try { int userId = getUserId(email); using (var db = new PassManagerContext()) { Account acc = db.Accounts .First(a => a.userID == userId && websiteUrl == a.websiteUrl); if (acc != null) { db.Accounts.Remove(acc); db.SaveChanges(); return(Json(new { succeded = true })); } else { return(Json(new { succeded = false, reason = "Failed to find item." })); } } } catch (Exception e) { return(Json(new { succeded = false, reason = "An error has occured" })); } }
public static void AddActivity(PassManagerContext db, User user, string jsonActivity) { Activity recent = JsonConvert.DeserializeObject <Activity>(jsonActivity); // add activity Settings settings = JsonConvert.DeserializeObject <Settings>(user.settings); List <Activity> activities = settings.recentActivity; if (activities.Count >= 10) { activities.RemoveAt(activities.Count - 1); } activities.Insert(0, recent); user.settings = JsonConvert.SerializeObject(settings); db.SaveChanges(); // alert user if (settings.alert) { MessageController.Alert(user, recent); } }
private User getUser(string email) { using (var db = new PassManagerContext()) { return(db.Users .Single(u => u.email.ToLower() == email.ToLower())); } }
private bool CheckEmailExists(string email) { using (var db = new PassManagerContext()) { var users = db.Users .Where(u => u.email.ToLower() == email.ToLower()) .ToList(); return(users.Any()); } }
private User getUser(string email) { try { using (var db = new PassManagerContext()) { return(db.Users .Single(u => u.email.ToLower() == email.ToLower())); } } catch (System.InvalidOperationException e) { return(null); } }
private bool UserExists(string email) { try { using (var db = new PassManagerContext()) { db.Users .Single(u => u.email.ToLower() == email.ToLower()); return(true); } } catch (InvalidOperationException e) { return(false); } }
public JsonResult updateName(string email, string name) { try { using (var db = new PassManagerContext()) { db.Users .Single(u => u.email.ToLower() == email.ToLower()) .name = name; db.SaveChanges(); return(Json(new { succeeded = true })); } } catch (Exception e) { return(Json(new { succeeded = false, reason = "An error occured" })); } }
public JsonResult Update(string settingsJson, string email) { try { using (var db = new PassManagerContext()) { User user = db.Users .Single(u => u.email.ToLower() == email.ToLower()); user.settings = settingsJson; db.SaveChanges(); return(Json(new { succeeded = true })); } } catch (Exception e) { return(Json(new { succeeded = false, reason = "An error occurred." })); } }
public JsonResult Issue(string email, string password, string jsonActivity) { try { using (var db = new PassManagerContext()) { User user = db.Users .Single(u => u.email.ToLower() == email.ToLower() && Hash.VerifyPBKDF2(password, u.passHash)); SettingsController.AddActivity(db, user, jsonActivity); return(Json(new { loggedIn = true, token = IssueToken(email) })); } } catch (System.InvalidOperationException e) { return(Json(new { loggedIn = false, reason = "Invalid email or password" })); } catch (Exception e) { return(Json(new { loggedIn = false, reason = "An error occured" })); } }
public JsonResult SignUp(string name, string email, string password) { try { // check if email already exists if (CheckEmailExists(email)) { return(Json(new { succeeded = false, reason = "Email Already Exists" })); } string hash = Hash.HashHexPBKDF2(password); // hash pass using (var db = new PassManagerContext()) { db.Users.Add(new User(name, email.ToLower(), hash)); db.SaveChanges(); return(Json(new { succeeded = true, token = TokenController.IssueToken(email) })); } } catch (Exception e) { return(Json(new { succeeded = false, reason = "An error occured." })); } }