Пример #1
0
        public void UpdatePersonaBarUserSettings(UserSettings settings, int userId, int portalId)
        {
            var controller          = new PersonalizationController();
            var personalizationInfo = controller.LoadProfile(userId, portalId);

            Personalization.SetProfile(personalizationInfo, ContainerName, UserSettingsKey, settings);
            controller.SaveProfile(personalizationInfo);
        }
Пример #2
0
            private static string GetLanguageDisplayMode(int portalId)
            {
                string viewTypePersonalizationKey = "LanguageDisplayMode:ViewType" + portalId;
                PersonalizationController personalizationController = new PersonalizationController();
                PersonalizationInfo       personalization           = personalizationController.LoadProfile(PortalSettings.Current.UserInfo.UserID, portalId);

                string viewType = Convert.ToString(personalization.Profile[viewTypePersonalizationKey]);

                return(string.IsNullOrEmpty(viewType) ? "NATIVE" : viewType);
            }
Пример #3
0
        public void EditModeForPage(int pageId, int userId)
        {
            var portalSettings = PortalController.Instance.GetCurrentPortalSettings();
            var newCookie      = new HttpCookie("LastPageId", $"{portalSettings.PortalId}:{pageId}")
            {
                Path = (!string.IsNullOrEmpty(Globals.ApplicationPath) ? Globals.ApplicationPath : "/")
            };

            HttpContext.Current.Response.Cookies.Add(newCookie);

            if (portalSettings.UserMode != PortalSettings.Mode.Edit)
            {
                var personalizationController = new PersonalizationController();
                var personalization           = personalizationController.LoadProfile(userId, portalSettings.PortalId);
                personalization.Profile["Usability:UserMode" + portalSettings.PortalId] = "EDIT";
                personalization.IsModified = true;
                personalizationController.SaveProfile(personalization);
            }
        }
        /// <summary>
        ///   Sets the current user so that checking authentication and roles works.
        /// </summary>
        /// <remarks>
        ///   Copies functionality from <c>DotNetNuke.HttpModules.Membership.MembershipModule.OnAuthenticateRequest</c>
        ///   to get the current user set as the "Current User"
        /// </remarks>
        private void SetCurrentUser()
        {
            // Obtain PortalSettings from Current Context
            var portalSettings = PortalController.GetCurrentPortalSettings();

            if (this.Context.Request.IsAuthenticated && portalSettings != null)
            {
                var roleController = new RoleController();
                var cachedUser     = UserController.GetCachedUser(portalSettings.PortalId, this.Context.User.Identity.Name);

                if (this.Context.Request.Cookies["portalaliasid"] != null)
                {
// ReSharper disable PossibleNullReferenceException
                    var portalCookie = FormsAuthentication.Decrypt(this.Context.Request.Cookies["portalaliasid"].Value);

                    // check if user has switched portals
                    if (portalSettings.PortalAlias.PortalAliasID != int.Parse(portalCookie.UserData))
                    {
                        // expire cookies if portal has changed
                        this.Context.Response.Cookies["portalaliasid"].Value   = null;
                        this.Context.Response.Cookies["portalaliasid"].Path    = "/";
                        this.Context.Response.Cookies["portalaliasid"].Expires = DateTime.Now.AddYears(-30);

                        this.Context.Response.Cookies["portalroles"].Value   = null;
                        this.Context.Response.Cookies["portalroles"].Path    = "/";
                        this.Context.Response.Cookies["portalroles"].Expires = DateTime.Now.AddYears(-30);

// ReSharper restore PossibleNullReferenceException
                    }
                }

                // authenticate user and set last login ( this is necessary for users who have a permanent Auth cookie set )
                if (cachedUser == null || cachedUser.IsDeleted || cachedUser.Membership.LockedOut ||
                    cachedUser.Membership.Approved == false ||
                    cachedUser.Username.ToLower() != this.Context.User.Identity.Name.ToLower())
                {
                    var portalSecurity = new PortalSecurity();
                    portalSecurity.SignOut();

                    // Remove user from cache
                    if (cachedUser != null)
                    {
                        DataCache.ClearUserCache(portalSettings.PortalId, this.Context.User.Identity.Name);
                    }

                    // Redirect browser back to home page
                    this.Context.Response.Redirect(this.Context.Request.RawUrl, true);
                    return;
                }

                // valid Auth cookie
                // if users LastActivityDate is outside of the UsersOnlineTimeWindow then record user activity
                if (
                    DateTime.Compare(
                        cachedUser.Membership.LastActivityDate.AddMinutes(Host.UsersOnlineTimeWindow), DateTime.Now) < 0)
                {
                    // update LastActivityDate and IP Address for user
                    cachedUser.Membership.LastActivityDate = DateTime.Now;
                    cachedUser.LastIPAddress = this.Context.Request.UserHostAddress;
                    UserController.UpdateUser(portalSettings.PortalId, cachedUser);
                }

                // refreshroles is set when a role is added to a user by an administrator
                bool refreshCookies = cachedUser.RefreshRoles;

                // check for RSVP code
                if (!cachedUser.RefreshRoles && this.Context.Request.QueryString["rsvp"] != null &&
                    string.IsNullOrEmpty(this.Context.Request.QueryString["rsvp"]) == false)
                {
                    foreach (RoleInfo objRole in roleController.GetPortalRoles(portalSettings.PortalId))
                    {
                        if (objRole.RSVPCode == this.Context.Request.QueryString["rsvp"])
                        {
                            roleController.UpdateUserRole(portalSettings.PortalId, cachedUser.UserID, objRole.RoleID);

                            // clear portalroles so the new role is added to the cookie below
                            refreshCookies = true;
                        }
                    }
                }

                // create cookies if they do not exist yet for this session.
                if (this.Context.Request.Cookies["portalroles"] == null || refreshCookies)
                {
                    // keep cookies in sync
                    var currentDateTime = DateTime.Now;

                    // create a cookie authentication ticket ( version, user name, issue time, expires every hour, don't persist cookie, roles )
                    var portalTicket = new FormsAuthenticationTicket(
                        1,
                        this.Context.User.Identity.Name,
                        currentDateTime,
                        currentDateTime.AddHours(1),
                        false,
                        portalSettings.PortalAlias.PortalAliasID.ToString());

                    // encrypt the ticket
                    string portalAliasId = FormsAuthentication.Encrypt(portalTicket);

// ReSharper disable PossibleNullReferenceException
                    // send portal cookie to client
                    this.Context.Response.Cookies["portalaliasid"].Value   = portalAliasId;
                    this.Context.Response.Cookies["portalaliasid"].Path    = "/";
                    this.Context.Response.Cookies["portalaliasid"].Expires = currentDateTime.AddMinutes(1);

// ReSharper restore PossibleNullReferenceException
                    // get roles from UserRoles table
                    string[] arrPortalRoles = roleController.GetRolesByUser(cachedUser.UserID, portalSettings.PortalId);

                    // create a string to persist the roles, attach a portalID so that cross-portal impersonation cannot occur
                    string strPortalRoles = portalSettings.PortalId + "!!" + string.Join(";", arrPortalRoles);

                    // create a cookie authentication ticket ( version, user name, issue time, expires every hour, don't persist cookie, roles )
                    var rolesTicket = new FormsAuthenticationTicket(
                        1,
                        this.Context.User.Identity.Name,
                        currentDateTime,
                        currentDateTime.AddHours(1),
                        false,
                        strPortalRoles);

                    // encrypt the ticket
                    string strRoles = FormsAuthentication.Encrypt(rolesTicket);

// ReSharper disable PossibleNullReferenceException
                    // send roles cookie to client
                    this.Context.Response.Cookies["portalroles"].Value   = strRoles;
                    this.Context.Response.Cookies["portalroles"].Path    = "/";
                    this.Context.Response.Cookies["portalroles"].Expires = currentDateTime.AddMinutes(1);

                    if (refreshCookies)
                    {
                        // if rsvp, update portalroles in context because it is being used later
                        this.Context.Request.Cookies["portalroles"].Value = strRoles;
                    }
                }

                if (this.Context.Request.Cookies["portalroles"] != null)
                {
                    // get roles from roles cookie
                    if (this.Context.Request.Cookies["portalroles"].Value != string.Empty)
                    {
                        var roleTicket = FormsAuthentication.Decrypt(this.Context.Request.Cookies["portalroles"].Value);

// ReSharper restore PossibleNullReferenceException
                        if (roleTicket != null)
                        {
                            // get the role data and split it into portalid and a string array of role data
                            string rolesdata = roleTicket.UserData;
                            char[] separator = "!!".ToCharArray();

                            // need to use StringSplitOptions.None to preserve case where superuser has no roles
                            string[] rolesParts = rolesdata.Split(separator, StringSplitOptions.None);

                            // if cookie is for a different portal than current force a refresh of roles else used cookie cached version
                            if (Convert.ToInt32(rolesParts[0]) != portalSettings.PortalId)
                            {
                                cachedUser.Roles = roleController.GetRolesByUser(cachedUser.UserID, portalSettings.PortalId);
                            }
                            else
                            {
                                cachedUser.Roles = rolesParts[2].Split(';');
                            }
                        }
                        else
                        {
                            cachedUser.Roles = roleController.GetRolesByUser(cachedUser.UserID, portalSettings.PortalId);
                        }

                        // Clear RefreshRoles flag
                        if (cachedUser.RefreshRoles)
                        {
                            cachedUser.RefreshRoles = false;
                            UserController.UpdateUser(portalSettings.PortalId, cachedUser);
                        }
                    }

                    // save userinfo object in context
                    this.Context.Items.Add("UserInfo", cachedUser);

                    // load the personalization object
                    var personalizationController = new PersonalizationController();
                    personalizationController.LoadProfile(this.Context, cachedUser.UserID, cachedUser.PortalID);

                    // Localization.SetLanguage also updates the user profile, so this needs to go after the profile is loaded
                    Localization.SetLanguage(cachedUser.Profile.PreferredLocale);
                }
            }

            if (HttpContext.Current.Items["UserInfo"] == null)
            {
                this.Context.Items.Add("UserInfo", new UserInfo());
            }
        }