⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userrolesadmin.cs

📁 本系统是在asp版《在线文件管理器》的基础上设计制作
💻 CS
字号:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CommunityServer;
using CommunityServer.Components;


namespace CommunityServer.Controls {

	public class UserRolesAdmin : TemplatedWebControl {

		#region Child Controls

		protected System.Web.UI.WebControls.ListBox listAvailableRoles;
		protected System.Web.UI.WebControls.Button btnAddRole;
		protected System.Web.UI.WebControls.Button btnRemoveRole;
		protected System.Web.UI.WebControls.ListBox listAssignedRoles;
		protected System.Web.UI.WebControls.Label lblUserName;
		protected System.Web.UI.WebControls.Button btnSave;

		#endregion

		#region Skin

		protected override void AttachChildControls() {
			
			this.listAvailableRoles = (ListBox)FindControl( "listAvailableRoles" );
			this.listAssignedRoles = (ListBox)FindControl( "listAssignedRoles" );
			this.btnAddRole = (Button)FindControl( "btnAddRole" );
			this.btnRemoveRole = (Button)FindControl( "btnRemoveRole" );
			this.btnSave = (Button)FindControl( "btnSave" );
			this.lblUserName = (Label)FindControl( "lblUserName" );

			InitializeChildControls();
		}

		private void InitializeChildControls() {

			btnSave.Click +=new EventHandler(btnSave_Click);
			btnAddRole.Click +=new EventHandler(btnAddRole_Click);
			btnRemoveRole.Click +=new EventHandler(btnRemoveRole_Click);

		}

		#endregion

		#region Command Handlers

		private void btnSave_Click(object sender, EventArgs e) {

			User user = Users.GetUser(CSContext.Current.UserID, false );

			//Create a container for the users current roles
			StringCollection existingRoles = new StringCollection();
			existingRoles.AddRange( Roles.GetUserRoleNames( user.Username, false ) );

			//loop through users assigned roles
			foreach(ListItem li in listAssignedRoles.Items) {
				//if the user is "STILL" in a role (previous and now), remove them from the temp collection
				if(existingRoles.Contains(li.Text)) {
					existingRoles.Remove(li.Text); 
				}
				else {
					//This role is not in the existing roles collection, so we add it
					Roles.AddUserToRole(user.Username,li.Text);
				}
			}

			//These are roles a user was in previously, but has been removed now.
			foreach(string role in existingRoles) {
				Roles.RemoveUserFromRole(user.Username,role);
			}

			Page.Response.Redirect( Globals.GetSiteUrls().AdminManageUsers );
		}

		private void btnAddRole_Click(object sender, EventArgs e) {
			SwapListItem( listAvailableRoles, listAssignedRoles );
			listAvailableRoles.SelectedIndex = -1;
			listAssignedRoles.SelectedIndex	= -1;
		}

		private void btnRemoveRole_Click(object sender, EventArgs e) {
			SwapListItem( listAssignedRoles, listAvailableRoles );
			listAvailableRoles.SelectedIndex = -1;
			listAssignedRoles.SelectedIndex	= -1;
		}

		#endregion

		#region Command Implementations

		#endregion

		protected override void OnLoad(EventArgs e) {
			if ( !Page.IsPostBack ) {
				this.DataBind();
			}
			base.OnLoad( e );
		}

		public override void DataBind(){
			base.DataBind();

			btnAddRole.Text = ResourceManager.GetString( "Add" ) + " ->";
			btnRemoveRole.Text = "<- " + ResourceManager.GetString( "Remove" );
			btnSave.Text = ResourceManager.GetString( "Save" );

			if( CSContext.Current.UserID <= 0 ) {
				btnAddRole.Enabled = false;
				btnRemoveRole.Enabled = false;
				btnSave.Enabled = false;
				return;
			}

			User user = Users.GetUser(CSContext.Current.UserID, false );

			lblUserName.Text = user.Username;

			// Lucian 4/7/04: Use clone here to prevent loading next time 
			// previous changed list from cache. Remove & RemoveAt seem to work 
			// directly on the cached list, so next time the list isn't the one should be.
			string[] userRoles = (string[])Roles.GetUserRoleNames( user.Username, false ).Clone();
			string[] availableRoles = (string[])Roles.GetRoleNames().Clone();

			StringCollection filter = new StringCollection();
			filter.AddRange(availableRoles);

			foreach(string role in userRoles) {
				if(filter.Contains(role)) {
					filter.Remove(role);
				}
			}

			availableRoles = new string[filter.Count];

			filter.CopyTo(availableRoles,0);

			listAvailableRoles.DataSource = availableRoles;
			listAvailableRoles.DataBind();

			listAssignedRoles.DataSource = userRoles;
			listAssignedRoles.DataBind();

		}

		protected void SwapListItem( ListBox source, ListBox destination ) {
			ListItem item = source.SelectedItem;

			if( item != null ) {
				destination.Items.Add( item );
				source.Items.Remove( item );
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -