+1-888-365-2779
Try Now
More in this section

Forums / Security / forum, redirect login and restrict access

forum, redirect login and restrict access

10 posts, 0 answered
  1. Roy
    Roy avatar
    28 posts
    Registered:
    20 Jan 2009
    22 Jul 2009
    Link to this post
    Hello,

    I'm using Sitefinity Community Editon (3.5 SP1) and have just setup my first forum. As anonymous posts are not allowed, I have added a login page to the sitemap and placed a login control on this page.

    When I try to add a thread or post I'm redirected to the login page. After providing the right credentials I am logged in but not redirected to the appropriate forum page. I can see that the returnURL is being set. When I browse back to the previous page and add a thread a post I'm able to do so (so I am authenticated). How do I solve this ?

    Another question: is it possible to restrict access to a specific forum? (e.g.: a project forum and only the projectmembers are allowed access to this forum). Would this be possible?

    Kind regards,

    Roy


  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    22 Jul 2009
    Link to this post
    Hi Roy,

    You can add the following logic to your login control:

    public partial class UserControls_FinalLoginControls_LoginControl : UserControl 
       void Page_Load(object sender, EventArgs e)  
        {  
            this.Login1.LoggedIn += new EventHandler(Login1_LoggedIn);  
        }  
      
        void Login1_LoggedIn(object sender, EventArgs e)  
        {  
            string redirectUrl = Request.QueryString["ReturnUrl"];  
            if (!string.IsNullOrEmpty(redirectUrl))  
            {  
                redirectUrl = HttpUtility.UrlDecode(redirectUrl);  
                this.Response.Redirect(redirectUrl, true);  
            }  
            else  
                this.Response.Redirect("~/home.aspx");  
        }  
        public Login Login 
        { 
            get 
            { 
                return this.Login1; 
            } 
            set 
            { 
                this.Login1 = value
            } 
        } 
     
     
     

    Note that you should set SingleThreadPage  property of Forums control.

    Another question: is it possible to restrict access to a specific forum? (e.g.: a project forum and only the projectmembers are allowed access to this forum). Would this be possible?

    You can check whether the logged user belongs to a certain role( or by name) that should access restricted forum. If not response an error message or redirect the user to another page.

       void Login1_LoggedIn(object sender, EventArgs e) 
       { 
           if (Login1.UserName.Equals("test")) 
           { 
               Response.Redirect("http://yahoo.com"); 
     
           } 
        .... 
          .... 
     


    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Roy
    Roy avatar
    28 posts
    Registered:
    20 Jan 2009
    22 Jul 2009
    Link to this post
    Hello Ivan,

    Thanks for your quick response. The login redirect now works!.

    About the 2nd answer I've got another question. How can you determine which forum the user is trying to enter. Let's say I have a forumlist with 3 forums:

    - forum A
    - forum B
    - forum C

    and the user is only allowed to acces forum C. How do you detect which forum the user is trying to access?
    And how would you restricts access in this scenario?

    Kind regards,

    Roy




  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    22 Jul 2009
    Link to this post
    Hi Roy,

    You can use GetForums method. For more information please take a look at our Developers Manual.

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  5. Roy
    Roy avatar
    28 posts
    Registered:
    20 Jan 2009
    22 Jul 2009
    Link to this post
    Ivan,

    Where would you place the code to check for a forum (single forum page?) and how can I add the code to controltemplate. Would I have to create a usercontrol (mysingleforum?) and add the code to this control.
    Then create a page, add the usercontrol and use this page as the destination page for the singleforum URL's?

    Kind regards, 

    Roy
     
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    24 Jul 2009
    Link to this post
    Hi Roy,

    Subscribe for ItemDatabound events of Repater and Grid controls in ForumList template. Then create a code behind for the template and manage the items.  The template is located under Sitefinity/ControlTemplates/Forums folder Below is a sample code:

    public partial class Sitefinity_ControlTemplates_Forums_ForumList : System.Web.UI.UserControl 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            this.Uncategorized.RowDataBound += new GridViewRowEventHandler(Uncategorized_RowDataBound); 
        } 
     
        void Uncategorized_RowDataBound(object sender, GridViewRowEventArgs e) 
        { 
            GridViewRow row = e.Row; 
            if (row.RowType == DataControlRowType.DataRow) 
            { 
                ForumManager manager = new ForumManager(); 
                IForum forum = manager.GetForum(((IForum) row.DataItem).ID); 
               // do something with the forum 
            } 
     
     
        } 
      
        // works for categorized forums 
        protected void ForumsRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) 
        { 
     
            RepeaterItem item = e.Item; 
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem) 
            { 
                ForumManager manager = new ForumManager(); 
                GridView inner = (GridView)item.FindControl("inner"); 
                IList posts = manager.GetForums(((Telerik.Forums.ICategory)item.DataItem).ID, true); 
                if(UserManager.IsCurrentUserInRole("test")) 
                    item.Visible = false
                inner.DataSource = posts; 
                inner.DataBind(); 
            } 
        } 
     


    All the best,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  7. Roy
    Roy avatar
    28 posts
    Registered:
    20 Jan 2009
    28 Jul 2009
    Link to this post
    Hi Ivan,

    Thanks for your sample code. I have one extra question. What if you want to hide a specific forum in a categorized forum. For example:

    Forum Category "websites"
        - forum "site A"
        - forum "site B"
    Forum Category "xyz"

    I would like to hide site B for certain user roles. I tried to implement this myself by iterate throught the list of forums and check the name and if the name meets a condition then remove that forum from the list. But this causes an error "the collection has changed" at runtime.

    Any ideas?

    Kind regards, 

    Roy
      

  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    28 Jul 2009
    Link to this post
    Hi Roy,

    You need to subscribe for RowDataBound event of GridView. Then get the forum by name and hide it or show it.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  9. Roy
    Roy avatar
    28 posts
    Registered:
    20 Jan 2009
    29 Jul 2009
    Link to this post

    Hello Ivan,

    With your support I finally managed to get it working. The issue I ran into is that for categorized forums, the gridview is inside a repeater. This makes it harder to handle the events of the gridview. I managed to solve this by adding a handler to the grid in code behind. For other users that may run into the same problem I have posted my code below.

    Kind regards,

    Roy

     

    Protected

     

    Sub forumsRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles forumsRepeater.ItemDataBound

     

     


        Dim
    item As RepeaterItem = e.Item

     

     

        If (item.ItemType = ListItemType.Item OrElse item.ItemType = ListItemType.AlternatingItem) Then

     

     

     

            Dim fman As New ForumManager
            ' Get the forums for this category

     

            listforums = fman.GetForums(

    CType(item.DataItem, ICategory).ID, True)
            ' Get the grid inside the repeater

     

     

            Dim inner As GridView = CType(item.FindControl("inner"), GridView)
            ' Add an handler so that each row can be checked (will fire for each row)
            AddHandler
    inner.RowDataBound, AddressOf CheckGridRow

     

            inner.DataSource = listforums

            inner.DataBind()

     

        End If

     

     

     

    End Sub

     

     

     

     

     

     

    Private Sub CheckGridRow(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
       
        'Check if user is in certain role
         
    If (UserManager.IsCurrentUserInRole("xyz")) Then

     

     

     

            Dim fm As New ForumManager

     

     

            Dim gvrow As GridViewRow = e.Row

     

     

            If gvrow.RowType = DataControlRowType.DataRow Then
                    ' Get the forum for the current row

     

     

     

                    Dim forum As IForum = fm.GetForum(CType(e.Row.DataItem, IForum).ID)
                    ' Check forum name

     

     

                    If forum.Name = "abc" Then

     

     

     

                        e.Row.Visible = True

     

     

     

                    Else

     

     

     

                        e.Row.Visible = False

     

     

     

                    End If

     

     

     

            End If

     

     

     

        End If

     

     

     

    End Sub

     

  10. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    29 Jul 2009
    Link to this post
    Hello Roy,

    The code seems to be correct. You can get the forum directly from e.Row.DataItem

    IForum frm = (IForum)e.Row.DataItem;        


    Greetings,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Register for webinar
10 posts, 0 answered