예제 #1
0
        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" }));
            }
        }
예제 #2
0
        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" }));
            }
        }
예제 #3
0
        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" }));
            }
        }
예제 #4
0
        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" }));
            }
        }
예제 #5
0
        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." }));
            }
        }
예제 #6
0
        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" }));
            }
        }
예제 #7
0
        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);
            }
        }
예제 #8
0
 private User getUser(string email)
 {
     using (var db = new PassManagerContext())
     {
         return(db.Users
                .Single(u => u.email.ToLower() == email.ToLower()));
     }
 }
예제 #9
0
        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());
            }
        }
예제 #10
0
 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);
     }
 }
예제 #11
0
        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);
            }
        }
예제 #12
0
        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" }));
            }
        }
예제 #13
0
        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." }));
            }
        }
예제 #14
0
        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" }));
            }
        }
예제 #15
0
        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." }));
            }
        }