public static void SetWatched(SiteDB db, long EpisodeId, long UserId, bool Watched)
 {
     //find the WatchedEpisode record.
     WatchedEpisode watchedEpisode = db.WatchedEpisodes.SingleOrDefault(oo => oo.EpisodeId == EpisodeId && oo.UserId == UserId);
     if (watchedEpisode != null)
     {
         //chech if we should mark watched or unwatched.
         if (!Watched)
         {
             //mark episode unwatched by deleting the WatchedEpisode record.
             db.WatchedEpisodes.Remove(watchedEpisode);
         }
     }
     else if (Watched)
     {
         //mark episode watched by adding a WatchedEpisode record.
         watchedEpisode = new WatchedEpisode();
         watchedEpisode.EpisodeId = EpisodeId;
         watchedEpisode.UserId = UserId;
         watchedEpisode.Created = DateTime.Now;
         db.WatchedEpisodes.Add(watchedEpisode);
     }
     //save changes.
     db.SaveChanges();
 }
        public static void AddUsersToRoles(SiteDB db, string[] Usernames, string[] RoleNames)
        {
            //get all roles
            List <Role> lstRoles = db.Roles.ToList();

            //loop thru users.
            foreach (String username in Usernames)
            {
                //get the user.
                User user = UserRepository.GetUser(db, username);
                if (user != null)
                {
                    //loop thru roles.
                    foreach (string rolename in RoleNames)
                    {
                        //find the roleid of the role we need to add.
                        Role role = lstRoles.SingleOrDefault(oo => oo.RoleName.ToLower() == rolename.ToLower());
                        if (role != null)
                        {
                            //check if the user already has this role.
                            if (!user.Roles.Contains(role))
                            {
                                //add the role.
                                user.Roles.Add(role);
                            }
                        }
                    }
                }
            }
            db.SaveChanges();
        }
 public static void AddUsersToRoles(SiteDB db, string[] Usernames, string[] RoleNames)
 {
     //get all roles
     List<Role> lstRoles = db.Roles.ToList();
     //loop thru users.
     foreach (String username in Usernames)
     {
         //get the user.
         User user = UserRepository.GetUser(db, username);
         if (user != null)
         {
             //loop thru roles.
             foreach (string rolename in RoleNames)
             {
                 //find the roleid of the role we need to add.
                 Role role = lstRoles.SingleOrDefault(oo => oo.RoleName.ToLower() == rolename.ToLower());
                 if (role != null)
                 {
                     //check if the user already has this role.
                     if (!user.Roles.Contains(role))
                     {
                         //add the role.
                         user.Roles.Add(role);
                     }
                 }
             }
         }
     }
     db.SaveChanges();
 }
        public static bool DeleteRole(SiteDB db, string RoleName, bool ErrorIfPopulated)
        {
            Role role = GetRole(db, RoleName);

            if (role != null)
            {
                if (ErrorIfPopulated) //  && GetUsersInRole(roleName).Length > 0)
                {
                    if (role.Users.Count() > 0)
                    {
                        throw new ApplicationException("Cannot delete a populated role.");
                    }
                }
                else
                {
                    //remove all users in this role (we shouldn't get here if throwOnPopulatedRole is true.
                    foreach (User user in role.Users)
                    {
                        role.Users.Remove(user);
                    }
                }

                //remove the role.
                db.Roles.Remove(role);

                db.SaveChanges();
                return(true);
            }
            else
            {
                throw new ApplicationException("Role does not exist.");
            }
        }
        public static bool DeleteRole(SiteDB db, string RoleName, bool ErrorIfPopulated)
        {
            Role role = GetRole(db, RoleName);
            if (role != null)
            {
                if (ErrorIfPopulated) //  && GetUsersInRole(roleName).Length > 0)
                {
                    if (role.Users.Count() > 0)
                    {
                        throw new ApplicationException("Cannot delete a populated role.");
                    }
                }
                else
                {
                    //remove all users in this role (we shouldn't get here if throwOnPopulatedRole is true.
                    foreach (User user in role.Users)
                    {
                        role.Users.Remove(user);
                    }
                }

                //remove the role.
                db.Roles.Remove(role);

                db.SaveChanges();
                return true;
            }
            else
            {
                throw new ApplicationException("Role does not exist.");
            }
        }
Exemple #6
0
        public static bool ResetPassword(SiteDB db, User User, string NewPassword)
        {
            //ok to change password.
            User.PasswordHash = CreatePasswordHash(NewPassword);
            db.SaveChanges();

            return(true);
        }
        public static Role CreateRole(SiteDB db, string RoleName)
        {
            Role role = new Role();
            role.RoleName = RoleName;
            db.Roles.Add(role);
            db.SaveChanges();

            return role;
        }
        public static Role CreateRole(SiteDB db, string RoleName)
        {
            Role role = new Role();

            role.RoleName = RoleName;
            db.Roles.Add(role);
            db.SaveChanges();

            return(role);
        }
Exemple #9
0
        public void LogIt(long userId, string activity)
        {
            var log = new UserActivity();

            log.Activity = activity;
            log.Created  = DateTime.Now;
            log.UserID   = userId;
            _db.UserActivitys.Add(log);
            _db.SaveChanges();
        }
Exemple #10
0
        public static PasswordReset DeleteResetCode(SiteDB db, string ResetCode)
        {
            //delete the reset code.
            PasswordReset passwordReset = db.PasswordResets.SingleOrDefault(oo => oo.ResetCode == ResetCode);

            if (passwordReset != null)
            {
                db.PasswordResets.Remove(passwordReset);
                db.SaveChanges();
            }

            return(passwordReset);
        }
        public static User CreateUser(SiteDB db, string Username, string Password, string Email)
        {
            //create a new user.
            User user = new User();
            user.Username = Username;
            user.Email = Email;
            user.Enabled = true;
            //create salt for password hash.
            user.PasswordHash = CreatePasswordHash(Password);
            user.Created = DateTime.Now;
            user.Updated = user.Created;

            db.Users.Add(user);
            db.SaveChanges();

            return user;
        }
 public static bool ChangePassword(SiteDB db, string Username, string OldPassword, string NewPassword)
 {
     bool bSuccess = false;
     User user = GetUser(db, Username);
     if (user != null)
     {
         //validate password by creating hash using salt.
         if (BCryptHelper.CheckPassword(OldPassword, user.PasswordHash))
         {
             //ok to change password.
             user.PasswordHash = CreatePasswordHash(NewPassword);
             db.SaveChanges();
             bSuccess = true;
         }
     }
     return bSuccess;
 }
Exemple #13
0
        public static bool ValidateUser(SiteDB db, string Username, string Password)
        {
            bool valid = false;
            User user  = GetUser(db, Username);

            if (user != null)
            {
                //validate password by creating hash using salt.
                if (BCryptHelper.CheckPassword(Password, user.PasswordHash))
                {
                    valid          = true;
                    user.LastLogin = DateTime.Now;
                    db.SaveChanges();
                }
            }
            return(valid);
        }
Exemple #14
0
        public static User CreateUser(SiteDB db, string Username, string Password, string Email)
        {
            //create a new user.
            User user = new User();

            user.Username = Username;
            user.Email    = Email;
            user.Enabled  = true;
            //create salt for password hash.
            user.PasswordHash = CreatePasswordHash(Password);
            user.Created      = DateTime.Now;
            user.Updated      = user.Created;

            db.Users.Add(user);
            db.SaveChanges();

            return(user);
        }
Exemple #15
0
        public static bool ChangePassword(SiteDB db, string Username, string OldPassword, string NewPassword)
        {
            bool bSuccess = false;
            User user     = GetUser(db, Username);

            if (user != null)
            {
                //validate password by creating hash using salt.
                if (BCryptHelper.CheckPassword(OldPassword, user.PasswordHash))
                {
                    //ok to change password.
                    user.PasswordHash = CreatePasswordHash(NewPassword);
                    db.SaveChanges();
                    bSuccess = true;
                }
            }
            return(bSuccess);
        }
Exemple #16
0
        public static User Create3rdPartyAuthUser(SiteDB db, string LoginId, string LoginToken, short UserTypeId, string Username)
        {
            //create a new user.
            User user = new User();
            user.LoginId = LoginId;
            user.LoginToken = LoginToken;
            user.UserTypeId = UserTypeId;
            user.Username = Username;
            //set dummy email.
            user.Email = "*****@*****.**";
            user.Enabled = true;
            user.Created = DateTime.Now;
            user.Updated = user.Created;

            db.Users.Add(user);
            db.SaveChanges();

            return user;
        }
 public static void RemoveUsersFromRoles(SiteDB db, string[] Usernames, string[] RoleNames)
 {
     foreach (string roleName in RoleNames)
     {
         //get the role
         Role role = GetRole(db, roleName);
         if (role != null)
         {
             foreach (string userName in Usernames)
             {
                 User user = role.Users.SingleOrDefault(uu => uu.Username.ToLower() == userName.ToLower());
                 if (user != null)
                 {
                     role.Users.Remove(user);
                 }
             }
         }
     }
     db.SaveChanges();
 }
Exemple #18
0
        public static User DeleteUser(SiteDB db, string Username, bool DeleteData)
        {
            User user = GetUser(db, Username);

            if (user == null)
            {
                throw new ApplicationException("User not found.");
            }
            if (DeleteData)
            {
                db.Users.Remove(user);
            }
            else
            {
                user.Enabled = false;
            }
            db.SaveChanges();

            return(user);
        }
Exemple #19
0
        public static User CompleteRegistration(SiteDB db, string Username, string FirstName, string LastName)
        {
            if (string.IsNullOrWhiteSpace(FirstName) || string.IsNullOrWhiteSpace(LastName))
            {
                throw new ApplicationException("First name or last name is required.");
            }

            //get the user that should have been created by the membership provider.
            User user = GetUser(db, Username);
            if (user == null)
            {
                throw new ApplicationException("The newly created User could not be found.");
            }

            //update values membership provider did not set.
            user.FirstName = FirstName;
            user.LastName = LastName;

            db.SaveChanges();

            return user;
        }
Exemple #20
0
        public static void SendForgotPassword(SiteDB db, User User, string ForgotPasswordUrlTemplate, string EmailTemplatesPath)
        {
            //delete existing PasswordResets
            foreach (PasswordReset pr in User.PasswordResets.ToList())
            {
                db.PasswordResets.Remove(pr);
            }
            //add new PasswordReset.
            PasswordReset passwordReset = new PasswordReset();

            passwordReset.ResetCode = RandomDataGenerator.GetRandomString(12);
            AuditableRepository.DefaultAuditableToNow(passwordReset);
            User.PasswordResets.Add(passwordReset);

            db.SaveChanges();

            string template = File.OpenText(EmailTemplatesPath + "ForgotPassword.html.cshtml").ReadToEnd();
            var    model    = new
            {
                Email             = User.Email,
                FirstName         = User.FirstName,
                LastName          = User.LastName,
                ForgotPasswordUrl = string.Format(ForgotPasswordUrlTemplate, passwordReset.ResetCode),
                ApplicationName   = SiteSettings.ApplicationName
            };
            string body = Razor.Parse(template, model);

            //email the invitation.
            MailMessage message = new MailMessage();

            message.To.Add(User.Email);
            message.Subject    = "Reset " + SiteSettings.ApplicationName + " Password";
            message.Body       = body;
            message.IsBodyHtml = true;

            SmtpClient client = new SmtpClient();

            client.Send(message);
        }
Exemple #21
0
        public static User CompleteRegistration(SiteDB db, string Username, string FirstName, string LastName)
        {
            if (string.IsNullOrWhiteSpace(FirstName) || string.IsNullOrWhiteSpace(LastName))
            {
                throw new ApplicationException("First name or last name is required.");
            }

            //get the user that should have been created by the membership provider.
            User user = GetUser(db, Username);

            if (user == null)
            {
                throw new ApplicationException("The newly created User could not be found.");
            }

            //update values membership provider did not set.
            user.FirstName = FirstName;
            user.LastName  = LastName;

            db.SaveChanges();

            return(user);
        }
        public static bool ResetPassword(SiteDB db, User User, string NewPassword)
        {
            //ok to change password.
            User.PasswordHash = CreatePasswordHash(NewPassword);
            db.SaveChanges();

            return true;
        }
        public static void SendForgotPassword(SiteDB db, User User, string ForgotPasswordUrlTemplate, string EmailTemplatesPath)
        {
            //delete existing PasswordResets
            foreach (PasswordReset pr in User.PasswordResets.ToList())
            {
                db.PasswordResets.Remove(pr);
            }
            //add new PasswordReset.
            PasswordReset passwordReset = new PasswordReset();
            passwordReset.ResetCode = RandomDataGenerator.GetRandomString(12);
            AuditableRepository.DefaultAuditableToNow(passwordReset);
            User.PasswordResets.Add(passwordReset);

            db.SaveChanges();

            string template = File.OpenText(EmailTemplatesPath + "ForgotPassword.html.cshtml").ReadToEnd();
            var model = new
            {
                Email = User.Email,
                FirstName = User.FirstName,
                LastName = User.LastName,
                ForgotPasswordUrl = string.Format(ForgotPasswordUrlTemplate, passwordReset.ResetCode),
                ApplicationName = SiteSettings.ApplicationName
            };
            string body = Razor.Parse(template, model);

            //email the invitation.
            MailMessage message = new MailMessage();
            message.To.Add(User.Email);
            message.Subject = "Reset " + SiteSettings.ApplicationName + " Password";
            message.Body = body;
            message.IsBodyHtml = true;

            SmtpClient client = new SmtpClient();
            client.Send(message);
        }
        public static PasswordReset DeleteResetCode(SiteDB db, string ResetCode)
        {
            //delete the reset code.
            PasswordReset passwordReset = db.PasswordResets.SingleOrDefault(oo => oo.ResetCode == ResetCode);
            if (passwordReset != null)
            {
                db.PasswordResets.Remove(passwordReset);
                db.SaveChanges();
            }

            return passwordReset;
        }
Exemple #25
0
 public static void RemoveUsersFromRoles(SiteDB db, string[] Usernames, string[] RoleNames)
 {
     foreach (string roleName in RoleNames)
     {
         //get the role
         Role role = GetRole(db, roleName);
         if (role != null)
         {
             foreach (string userName in Usernames)
             {
                 User user = role.Users.SingleOrDefault(uu => uu.Username.ToLower() == userName.ToLower());
                 if (user != null)
                 {
                     role.Users.Remove(user);
                 }
             }
         }
     }
     db.SaveChanges();
 }
 /// <summary>
 /// If userIsOnline is true it updates the LastLoginDate.
 /// </summary>
 /// <param name="db"></param>
 /// <param name="user"></param>
 /// <param name="userIsOnline"></param>
 /// <remarks>EDIT ON REUSE: User</remarks>
 private void UpdateLastLogin(SiteDB db, User user, bool userIsOnline)
 {
     if (userIsOnline && user != null)
     {
         user.LastLogin = DateTime.Now;
         db.SaveChanges();
     }
 }
        public override string ResetPassword(string username, string answer)
        {
            //reset the users password to a temporary one.
            using (SiteDB db = new SiteDB())
            {
                User user = UserRepository.GetUser(db, username);
                if (user != null)
                {
                    //create a new password, hash it and save it.
                    string sPassword = Membership.GeneratePassword(8, 1);
                    user.PasswordSalt = UserRepository.CreateSalt();
                    user.PasswordHash = UserRepository.CreatePasswordHash(sPassword, user.PasswordSalt);
                    db.SaveChanges();

                    return sPassword;
                }
                else
                {
                    throw new ProviderException("The user could not be found.");
                }
            }
        }
        public static Series AddSeries(SiteDB db, long UserId, int TVDBSeriesId)
        {
            //check if series exists and if it does make sure the last update was at least one day ago.
            Series series = db.Serieses.Include("Episodes").SingleOrDefault(oo => oo.TVDBSeriesId == TVDBSeriesId);
            if (series == null || series.Updated < DateTime.Now.AddDays(-1))
            {
                TvdbSeries tvdbSeries = TVDBRepository.GetTvdbHandler().GetSeries(TVDBSeriesId, TvdbLanguage.DefaultLanguage, true, false, false);
                if (tvdbSeries == null)
                {
                    throw new ApplicationException("Could not find Series.");
                }

                //add the series from TVDB if it doesn't exist.
                if (series == null)
                {
                    //create series if it doesn't exist in local db.
                    if (series == null)
                    {
                        //add the series to our DB.
                        series = new Series();
                        series.TVDBSeriesId = tvdbSeries.Id;
                        series.Created = DateTime.Now;
                        db.Serieses.Add(series);
                        series.Episodes = new List<Episode>();
                    }
                }
                //check for changes.
                if (series.SeriesName != tvdbSeries.SeriesName)
                {
                    //update values.
                    series.SeriesName = tvdbSeries.SeriesName;
                    series.Updated = DateTime.Now;
                }

                //update/add epsidoes.
                List<Episode> seriesEpisodes = series.Episodes.ToList();
                //if (series.Episodes != null)
                //{
                //    seriesEpisodes = series.Episodes.ToList();
                //}
                //else
                //{
                //    seriesEpisodes = new List<Episode>();
                //}
                foreach (TvdbEpisode tvdpEpisode in tvdbSeries.Episodes)
                {
                    Episode episode = seriesEpisodes.SingleOrDefault(oo => oo.TVDBEpisodeId == tvdpEpisode.Id);
                    if (episode == null)
                    {
                        episode = new Episode();
                        episode.TVDBEpisodeId = tvdpEpisode.Id;
                        episode.Created = DateTime.Now;
                        series.Episodes.Add(episode);
                        //episode.SeriesId = series.SeriesId;
                        //db.Episodes.Add(episode);
                    }
                    //check for changes.
                    if (episode.EpisodeTitle != tvdpEpisode.EpisodeName
                        || episode.EpisodeNumber != tvdpEpisode.EpisodeNumber
                        || episode.Season != tvdpEpisode.SeasonNumber)
                    {
                        //at least one change so update values.
                        episode.EpisodeTitle = tvdpEpisode.EpisodeName;
                        episode.EpisodeNumber = tvdpEpisode.EpisodeNumber;
                        episode.Season = tvdpEpisode.SeasonNumber;
                        episode.Updated = DateTime.Now;
                    }
                }

                db.SaveChanges();
            }

            return series;
        }
        public static void StopWatchingSeries(SiteDB db, long UserId, long SeriesId)
        {
            ////delete all watched episodes.
            //grab all watched episodes for this series & user.
            List<WatchedEpisode> lstWatchedEpisodes = db.WatchedEpisodes.Where(oo => oo.Episode.SeriesId == SeriesId && oo.UserId == UserId).ToList();

            foreach (WatchedEpisode we in lstWatchedEpisodes)
            {
                db.WatchedEpisodes.Remove(we);
            }

            ////remove WatchedSeries record.
            WatchedSeries watchedSeries = db.WatchedSerieses.SingleOrDefault(oo => oo.SeriesId == SeriesId && oo.UserId == UserId);
            if (watchedSeries != null)
            {
                db.WatchedSerieses.Remove(watchedSeries);
            }
            db.SaveChanges();
        }
 public static void WatchSeries(SiteDB db, long UserId, long SeriesId)
 {
     ////add WatchedSeries record.
     //check if we're watching it yet.
     WatchedSeries watchedSeries = db.WatchedSerieses.SingleOrDefault(oo => oo.SeriesId == SeriesId && oo.UserId == UserId);
     if (watchedSeries == null)
     {
         watchedSeries = new WatchedSeries { UserId = UserId, SeriesId = SeriesId };
         AuditableRepository.DefaultAuditableToNow(watchedSeries);
         db.WatchedSerieses.Add(watchedSeries);
         db.SaveChanges();
     }
 }
        public static void SetWatched(SiteDB db, long SeriesId, long[] EpisodeIds, long UserId)
        {
            ////take the array of Ids and mark them as watched. Any WatchedEpisodes not in the list should be marked as unwatched by removing them.

            //grab all watched episodes for this series & user.
            List<WatchedEpisode> lstWatchedEpisodes = db.WatchedEpisodes.Where(oo => oo.Episode.SeriesId == SeriesId && oo.UserId == UserId).ToList();

            if (EpisodeIds != null)
            {
                //loop threw new list of watched Episodes
                foreach (long id in EpisodeIds)
                {
                    if (lstWatchedEpisodes.Count(oo => oo.EpisodeId == id) == 0)
                    {
                        //add new record.
                        WatchedEpisode newWatchedEpisode = new WatchedEpisode();
                        newWatchedEpisode.EpisodeId = id;
                        newWatchedEpisode.UserId = UserId;
                        newWatchedEpisode.Created = DateTime.Now;

                        db.WatchedEpisodes.Add(newWatchedEpisode);
                    }
                }
            }

            //delete all records not in new list of watched Episodes
            foreach (WatchedEpisode we in lstWatchedEpisodes.Where(oo => EpisodeIds == null || !EpisodeIds.Contains(oo.EpisodeId)))
            {
                db.WatchedEpisodes.Remove(we);
            }

            //save.
            db.SaveChanges();
        }
        public static void SetWatched(SiteDB db, long SeriesId, int Season, long UserId, bool Watched)
        {
            ////take the array of Ids and mark them as watched. Any WatchedEpisodes not in the list should be marked as unwatched by removing them.

            //grab all watched episodes for this series, user, and Season.
            List<WatchedEpisode> lstWatchedEpisodes = db.WatchedEpisodes.Where(oo => oo.Episode.SeriesId == SeriesId && oo.UserId == UserId && oo.Episode.Season == Season).ToList();

            //get list of EpisodeIds based on Season.
            List<long> lstEpisodeIds = new List<long>();

            //get list of EpisodeIds based on Season if we're marking as Watched.
            if (Watched)
            {
                lstEpisodeIds = db.Episodes.Where(oo => oo.SeriesId == SeriesId && oo.Season == Season).Select(oo => oo.EpisodeId).ToList();
            }

            //loop threw new list of watched Episodes
            foreach (long id in lstEpisodeIds)
            {
                if (lstWatchedEpisodes.Count(oo => oo.EpisodeId == id) == 0)
                {
                    //add new record.
                    WatchedEpisode newWatchedEpisode = new WatchedEpisode();
                    newWatchedEpisode.EpisodeId = id;
                    newWatchedEpisode.UserId = UserId;
                    newWatchedEpisode.Created = DateTime.Now;

                    db.WatchedEpisodes.Add(newWatchedEpisode);
                }
            }

            //delete all records not in new list of watched Episodes
            foreach (WatchedEpisode we in lstWatchedEpisodes.Where(oo => !lstEpisodeIds.Contains(oo.EpisodeId)))
            {
                db.WatchedEpisodes.Remove(we);
            }

            //save.
            db.SaveChanges();
        }
Exemple #33
0
        public static User DeleteUser(SiteDB db, string Username, bool DeleteData)
        {
            User user = GetUser(db, Username);
            if (user == null)
            {
                throw new ApplicationException("User not found.");
            }
            if (DeleteData)
            {
                db.Users.Remove(user);
            }
            else
            {
                user.Enabled = false;
            }
            db.SaveChanges();

            return user;
        }
Exemple #34
0
 public static bool ValidateUser(SiteDB db, string Username, string Password)
 {
     bool valid = false;
     User user = GetUser(db, Username);
     if (user != null)
     {
         //validate password by creating hash using salt.
         if (CreatePasswordHash(Password, user.PasswordSalt) == user.PasswordHash)
         {
             valid = true;
             user.LastLogin = DateTime.Now;
             db.SaveChanges();
         }
     }
     return valid;
 }