exhumed
  •  exhumed
  • 54.4% (Neutral)
  • YAF Camper Topic Starter
2014-04-23T13:17:03Z
Hi,

I have a site built around the sample web application.

Users register as normal to the site, but to restrict access to certain "members area" outside of the forum I use a custom application web form.

I want to assign a user who submits a valid application to the role "Applying", which has a roleID of 9 in the database, and I was wondering if this test code below is an acceptable way of doing it?

Sample aspx page
Quote:


<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="RoleTest.aspx.cs" Inherits="YAF.SampleWebApplication.RoleTest" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button_addRole" runat="server" Text="Add to Role" CssClass="btn btn-success" OnClick="Button_addRole_Click" />
<asp:Button ID="Button_removeRole" runat="server" Text="Remove Role" CssClass="btn btn-warning" OnClick="Button_removeRole_Click"/>
<br />
<asp:Label ID="Label_isApplying" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>



code behind file

Quote:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YAF.Core;
using YAF.Types.Constants;
using YAF.Types.EventProxies;
using YAF.Types.Interfaces;
using YAF.Types.Interfaces.Data;
using YAF.Core.Data;
using YAF.Controls;
using YAF.Types.Extensions;

namespace YAF.SampleWebApplication
{
public partial class RoleTest : System.Web.UI.Page
{
EditUsersGroups editUG = new EditUsersGroups();

protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button_addRole_Click(object sender, EventArgs e)
{
roleUpdateFromExternalSite(9,"Applying",true);
}

protected void Button_removeRole_Click(object sender, EventArgs e)
{
roleUpdateFromExternalSite(9, "Applying", false);
}

private void roleUpdateFromExternalSite(int roleID, string roleName, Boolean roleUpdate)
{

int CurrentUserID = Utility.GetUserID_int();

if (!UserMembershipHelper.IsGuestUser(CurrentUserID))
{
// save user in role
editUG.Get<IDbFunction>().Query.usergroup_save(CurrentUserID, roleID, roleUpdate);

// empty out access table
editUG.Get<IDbFunction>().Query.activeaccess_reset();

// get user's name
string userName = UserMembershipHelper.GetUserNameFromID(CurrentUserID);

// add/remove user from roles in membership provider
if (roleUpdate && !RoleMembershipHelper.IsUserInRole(userName, roleName))
{
RoleMembershipHelper.AddUserToRole(userName, roleName);
}
else if (!roleUpdate && RoleMembershipHelper.IsUserInRole(userName, roleName))
{
RoleMembershipHelper.RemoveUserFromRole(userName, roleName);
}

// Clearing cache with old permisssions data...
editUG.Get<IDataCache>().Remove(Constants.Cache.ActiveUserLazyData.FormatWith(CurrentUserID));


// update forum moderators cache just in case something was changed...
editUG.Get<IDataCache>().Remove(Constants.Cache.ForumModerators);

// clear the cache for this user...
editUG.Get<IRaiseEvent>().Raise(new UpdateUserEvent(CurrentUserID));

if(RoleMembershipHelper.IsUserInRole(userName, roleName))
{
Label_isApplying.Text = "User is Applying";
}
else
{
Label_isApplying.Text = "User is NOT Applying";
}

}
}

}
}



This appears to work, as the label gets updated successfully to indicate the user has been sucessfully added to the role.

What I'm asking is would this cause me problems down the line, or does it appear valid to you yaf Gurus? 🙂

Thanks for any help or advice

Exhumed
Sponsor

About Us

The YAF.NET is an open source .NET forum project. YAF.NET is supported by an team of international developers who are build community by building community software.

Powered by Resharper Donate with PayPal button

Project Twitter Updates

Copyright © YetAnotherForum.NET & Ingo Herbote. All rights reserved