YAFLogo

exhumed
  • exhumed
  • 54.4% (Neutral)
  • YAF Camper Topic Starter
11 years ago
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

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="RoleTest.aspx.cs" Inherits="YAF.SampleWebApplication.RoleTest" %>


code behind file

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().Query.usergroup_save(CurrentUserID, roleID, roleUpdate);

// empty out access table

editUG.Get().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().Remove(Constants.Cache.ActiveUserLazyData.FormatWith(CurrentUserID));

// update forum moderators cache just in case something was changed...

editUG.Get().Remove(Constants.Cache.ForumModerators);

// clear the cache for this user...

editUG.Get().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