Exemplo n.º 1
0
 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();
 }
Exemplo n.º 2
0
 /// <summary>
 /// Return how we want to display the user's name in the UI. Currently set to Username, could be changed to First/Last, etc,
 /// </summary>
 /// <param name="FormsAuthService"></param>
 /// <returns></returns>
 public static string GetUserFriendlyName(IFormsAuthenticationService FormsAuthService)
 {
     return HttpContext.Current.Cache.GetOrStore<string>(
         GetUserSignInKey(FormsAuthService, UserFriendlyNameKey),
         () =>
         {
             if (FormsAuthService.IsAuthenticated())
             {
                 using (SiteDB db = new SiteDB())
                 {
                     var user = UserRepository.GetUser(db, FormsAuthService.GetCurrentUserId());
                     if (user != null)
                     {
                         return user.Username;
                     }
                     else
                     {
                         return "";
                     }
                 }
             }
             else
             {
                 return "";
             }
         }
     );
 }
Exemplo n.º 3
0
 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();
 }
Exemplo n.º 4
0
        public TwitterController(IFormsAuthenticationService FormsAuthService)
        {
            _db = new SiteDB();
            _log = new UserActivity(_db);

            this.FormsAuthService = FormsAuthService;
        }
Exemplo n.º 5
0
        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.");
            }
        }
Exemplo n.º 6
0
        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();
        }
 /// <summary>
 /// Adds the specified user names to the specified roles for the configured applicationName.
 /// </summary>
 /// <param name="usernames">A string array of user names to be added to the specified roles.</param>
 /// <param name="roleNames">A string array of the role names to add the specified user names to.</param>
 public override void AddUsersToRoles(string[] usernames, string[] roleNames)
 {
     using (SiteDB db = new SiteDB())
     {
         RoleRepository.AddUsersToRoles(db, usernames, roleNames);
     }
 }
        public SessionController(IFormsAuthenticationService FormsAuthService, IMembershipService MembershipService)
        {
            _db = new SiteDB();
            _log = new UserActivity(_db);

            this.FormsAuthService = FormsAuthService;
            this.MembershipService = MembershipService;
        }
Exemplo n.º 9
0
 public static IQueryable<User> FindUsersInRole(SiteDB db, string RoleName)
 {
     return from uu in db.Users
            where uu.Roles.Any(rr => rr.RoleName.ToLower() == RoleName.ToLower())
             && uu.Enabled == true
            orderby uu.Username
            select uu;
 }
Exemplo n.º 10
0
        public static Role CreateRole(SiteDB db, string RoleName)
        {
            Role role = new Role();
            role.RoleName = RoleName;
            db.Roles.Add(role);
            db.SaveChanges();

            return role;
        }
Exemplo n.º 11
0
        public HomeController(IFormsAuthenticationService FormsAuthService)
        {
            _db = new SiteDB();
            _log = new UserActivity(_db);

            this.FormsAuthService = FormsAuthService;

            //since CurrentUserId is used a lot save it in a variable right away for easier to read code.
            if (System.Web.HttpContext.Current.Request.IsAuthenticated)
            {
                CurrentUserId = FormsAuthService.GetCurrentUserId();
            }
        }
Exemplo n.º 12
0
        public static List<WatchedEpisodeStatus> GetAllEpisodes(SiteDB db, long UserId, long SeriesId)
        {
            //can't seem to get outer joins to default values properly so get all watched episodes for this series/user.
            List<long> lstWatchedEpisodeIds = db.WatchedEpisodes.Where(oo => oo.Episode.SeriesId == SeriesId && oo.UserId == UserId).Select(oo => oo.EpisodeId).ToList();

            //due to some LINQ to Entities limitations we have to generate an anonymous type and then transform into into a WatchedEpisodeStatus.
            //http://samuelmueller.com/2009/11/working-with-projections-and-dtos-in-wcf-data-services/
            return (from ee in db.Episodes
                    where ee.SeriesId == SeriesId
                    select ee).ToList()
                    .Select(ee => new WatchedEpisodeStatus { Created = ee.Created, EpisodeId = ee.EpisodeId, EpisodeNumber = ee.EpisodeNumber, EpisodeTitle = ee.EpisodeTitle, Season = ee.Season, SeriesId = ee.SeriesId, TVDBEpisodeId = ee.TVDBEpisodeId, Updated = ee.Updated, UserId = UserId, Watched = (lstWatchedEpisodeIds.Contains(ee.EpisodeId)) }).ToList();

            //return db.WatchedEpisodes.Include("Episode").Where(oo => oo.UserId == UserId && oo.Episode.SeriesId == SeriesId).OrderBy(oo => oo.Episode.Season).OrderBy(oo => oo.Episode.EpisodeNumber);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Adds a new role to the data source for the configured applicationName.
        /// </summary>
        /// <param name="roleName">The name of the role to create.</param>
        public override void CreateRole(string roleName)
        {
            if (string.IsNullOrEmpty(roleName))
                throw new ProviderException("Role name cannot be empty or null.");
            if (roleName.IndexOf(',') > 0)
                throw new ArgumentException("Role names cannot contain commas.");
            if (roleName.Length > 255)
                throw new ProviderException("Role name cannot exceed 255 characters.");
            if (RoleExists(roleName))
                throw new ProviderException("Role name already exists.");

            using (SiteDB db = new SiteDB())
            {
                RoleRepository.CreateRole(db, roleName);
            }
        }
Exemplo n.º 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;
        }
Exemplo n.º 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;
 }
Exemplo n.º 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;
        }
Exemplo n.º 17
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;
        }
Exemplo n.º 18
0
 public static bool IsUserInRole(SiteDB db, string Username, string RoleName)
 {
     return(FindUsersInRole(db, RoleName, Username).Count() == 1);
 }
Exemplo n.º 19
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();
 }
Exemplo n.º 20
0
 /// <summary>
 /// Gets a value indicating whether the specified role name already exists in the role data source for the configured applicationName.
 /// </summary>
 /// <param name="roleName">The name of the role to search for in the data source.</param>
 /// <returns>
 /// true if the role name already exists in the data source for the configured applicationName; otherwise, false.
 /// </returns>
 public override bool RoleExists(string roleName)
 {
     using (SiteDB db = new SiteDB())
     {
         return RoleRepository.RoleExists(db, roleName);
     }
 }
Exemplo n.º 21
0
 /// <summary>
 /// Gets a value indicating whether the specified user is in the specified role for the configured applicationName.
 /// </summary>
 /// <param name="username">The user name to search for.</param>
 /// <param name="roleName">The role to search in.</param>
 /// <returns>
 /// true if the specified user is in the specified role for the configured applicationName; otherwise, false.
 /// </returns>
 public override bool IsUserInRole(string username, string roleName)
 {
     using (SiteDB db = new SiteDB())
     {
         return RoleRepository.IsUserInRole(db, username, roleName);
     }
 }
Exemplo n.º 22
0
 /// <summary>
 /// Gets a list of the roles that a specified user is in for the configured applicationName.
 /// </summary>
 /// <param name="username">The user to return a list of roles for.</param>
 /// <returns>
 /// A string array containing the names of all the roles that the specified user is in for the configured applicationName.
 /// </returns>
 public override string[] GetRolesForUser(string username)
 {
     using (SiteDB db = new SiteDB())
     {
         return RoleRepository.GetRolesForUser(db, username).Select(rr => rr.RoleName).ToArray();
     }
 }
Exemplo n.º 23
0
 public static User GetUser(SiteDB db, long UserId)
 {
     return(GetUser(db, UserId, false));
 }
Exemplo n.º 24
0
 public static bool RoleExists(SiteDB db, string RoleName)
 {
     return(db.Roles.Where(rr => rr.RoleName.ToLower() == RoleName.ToLower()).Count() == 1);
 }
Exemplo n.º 25
0
 public UserActivity(SiteDB db)
 {
     _db = db;
 }
Exemplo n.º 26
0
 public static IQueryable <Role> GetRolesForUser(SiteDB db, string Username)
 {
     return(from rr in db.Roles
            where rr.Users.Any(uu => uu.Username.ToLower() == Username.ToLower() && uu.Enabled == true)
            select rr);
 }
Exemplo n.º 27
0
 public static bool RoleExists(SiteDB db, string RoleName)
 {
     return db.Roles.Where(rr => rr.RoleName.ToLower() == RoleName.ToLower()).Count() == 1;
 }
Exemplo n.º 28
0
 public static Role GetRole(SiteDB db, string RoleName)
 {
     return(db.Roles.SingleOrDefault(oo => oo.RoleName.ToLower() == RoleName.ToLower()));
 }
Exemplo n.º 29
0
 /// <summary>
 /// Will return a User if the specified Username matches with the Email or Username fields.
 /// </summary>
 /// <param name="db"></param>
 /// <param name="Username"></param>
 /// <param name="IncludeDisabled"></param>
 /// <returns></returns>
 public static User GetUser(SiteDB db, string Username, bool IncludeDisabled)
 {
     return(db.Users.SingleOrDefault(oo => (oo.Username.ToLower() == Username.ToLower() || oo.Email.ToLower() == Username.ToLower()) && (IncludeDisabled || oo.Enabled == true)));
 }
Exemplo n.º 30
0
        /// <summary>
        /// Gets a list of all the roles for the configured applicationName.
        /// </summary>
        /// <returns>
        /// A string array containing the names of all the roles stored in the data source for the configured applicationName.
        /// </returns>
        public override string[] GetAllRoles()
        {
            using (SiteDB db = new SiteDB())
            {
                var qRoles = from rr in db.Roles
                             select rr.RoleName;

                return qRoles.ToArray();
            }
        }
Exemplo n.º 31
0
 /// <summary>
 /// Gets an array of user names in a role where the user name contains the specified user name to match.
 /// </summary>
 /// <param name="db"></param>
 /// <param name="RoleName"></param>
 /// <param name="UsernameQuery">Partial username.</param>
 /// <returns></returns>
 public static IQueryable<User> FindUsersInRole(SiteDB db, string RoleName, string UsernameQuery)
 {
     return from uu in FindUsersInRole(db, RoleName)
            where uu.Username.ToLower().Contains(UsernameQuery.ToLower())
            orderby uu.Username
            select uu;
 }
Exemplo n.º 32
0
 /// <summary>
 /// Gets a list of users in the specified role for the configured applicationName.
 /// </summary>
 /// <param name="roleName">The name of the role to get the list of users for.</param>
 /// <returns>
 /// A string array containing the names of all the users who are members of the specified role for the configured applicationName.
 /// </returns>
 public override string[] GetUsersInRole(string roleName)
 {
     using (SiteDB db = new SiteDB())
     {
         return RoleRepository.FindUsersInRole(db, roleName).Select(uu => uu.Email).ToArray();
     }
 }
Exemplo n.º 33
0
 public static Role GetRole(SiteDB db, string RoleName)
 {
     return db.Roles.SingleOrDefault(oo => oo.RoleName.ToLower() == RoleName.ToLower());
 }
Exemplo n.º 34
0
 /// <summary>
 /// Removes the specified user names from the specified roles for the configured applicationName.
 /// </summary>
 /// <param name="usernames">A string array of user names to be removed from the specified roles.</param>
 /// <param name="roleNames">A string array of role names to remove the specified user names from.</param>
 public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
 {
     using (SiteDB db = new SiteDB())
     {
         RoleRepository.RemoveUsersFromRoles(db, usernames, roleNames);
     }
     return;
 }
Exemplo n.º 35
0
 public static IQueryable<Role> GetRolesForUser(SiteDB db, string Username)
 {
     return from rr in db.Roles
            where rr.Users.Any(uu => uu.Username.ToLower() == Username.ToLower() && uu.Enabled == true)
            select rr;
 }
Exemplo n.º 36
0
 public static User GetUser(SiteDB db, long UserId, bool IncludeDisabled)
 {
     return(db.Users.SingleOrDefault(oo => (oo.UserId == UserId) && (IncludeDisabled || oo.Enabled == true)));
 }
Exemplo n.º 37
0
 public static bool IsUserInRole(SiteDB db, string Username, string RoleName)
 {
     return FindUsersInRole(db, RoleName, Username).Count() == 1;
 }
Exemplo n.º 38
0
 /// <summary>
 /// Removes a role from the data source for the configured applicationName.
 /// </summary>
 /// <param name="roleName">The name of the role to delete.</param>
 /// <param name="throwOnPopulatedRole">If true, throw an exception if <paramref name="roleName"/> has one or more members and do not delete <paramref name="roleName"/>.</param>
 /// <returns>
 /// true if the role was successfully deleted; otherwise, false.
 /// </returns>
 public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
 {
     using (SiteDB db = new SiteDB())
     {
         try
         {
             return RoleRepository.DeleteRole(db, roleName, throwOnPopulatedRole);
         }
         catch (ApplicationException exp)
         {
             throw new ProviderException(exp.Message, exp);
         }
     }
 }
Exemplo n.º 39
0
 /// <summary>
 /// Will return a User if the specified Username matches with the Email or Username fields.
 /// </summary>
 /// <param name="db"></param>
 /// <param name="Username"></param>
 /// <returns></returns>
 public static User GetUser(SiteDB db, string Username)
 {
     return(GetUser(db, Username, false));
 }