protected void Page_Load(object sender, EventArgs e)
 {
     SessionObject sessionObject = (SessionObject)Session["SessionObject"];
     _user = sessionObject.SelectedUser;
     if (!Page.IsPostBack)
     {
         LoadPermissions();
         LoadRoles();
         LoadPricingModules();
         LoadSchools();
         if (_user != null)
         {
             LoadUserRolesForEditing(_user.UserId);
             LoadUserSchoolsForEditing(_user.UserId);
             LoadUserPermissionsForEditing(_user.UserId);
         }
     }
 }
 protected void GetUser(string username)
 {
     _user = new ThinkgateUser(Membership.GetUser(username));
 }
        protected void rtlUserPermissionsForEditing_NeedDataSource(object sender, TreeListNeedDataSourceEventArgs e)
        {
            SessionObject sessionObject = (SessionObject)Session["SessionObject"];
            _user = sessionObject.SelectedUser;

            if (_user == null) return;
            ThinkgateUser user = ThinkgateUser.GetThinkgateUserByID(_user.UserId);
            DataTable table = user.GetUserPermissionsForEditing();
            rtlUserPermissionsForEditing.DataSource = table;
        }
        protected void rtlUserPermissionsForEditing_UpdateCommand(Object source, TreeListCommandEventArgs e)
        {
            SessionObject sessionObject = (SessionObject)Session["SessionObject"];
            _user = sessionObject.SelectedUser;
            if (_user == null) return;

            var editedItem = e.Item as TreeListDataItem;
            if (editedItem == null) return;

            //Get the new values:
            var newValues = new Hashtable();
            e.Item.OwnerTreeList.ExtractValuesFromItem(newValues, editedItem, true);
            Guid permissionID = new Guid();
            Guid.TryParse(newValues["PermissionId"].ToString(), out permissionID);
            if (permissionID == default(Guid)) return;

            string permissionName = newValues["PermissionName"] == null ? string.Empty : newValues["PermissionName"].ToString();
            int permissionLevel = newValues["PermissionLevelValue"] == null ? 0 : DataIntegrity.ConvertToInt(newValues["PermissionLevelValue"].ToString());
            bool hasPermission = newValues["HasPermission"] != null && DataIntegrity.ConvertToBool(newValues["HasPermission"]);

            ThinkgatePermission permission = new ThinkgatePermission(permissionID);

            if (hasPermission)
            {
                permission.PermissionLevel = (ThinkgatePermission.PermissionLevelValues)Enum.ToObject(typeof(ThinkgatePermission.PermissionLevelValues), permissionLevel);
                _user.addPermission(permission);
                lblResultMessage.Text = string.Format("Permission {0} was added to user {1}.", permissionName, _user.UserName);
                foreach (TreeListDataItem item in editedItem.ChildItems)
                {
                    Guid.TryParse(item.GetDataKeyValue("PermissionId").ToString(), out permissionID);
                    if (permissionID == default(Guid)) return;
                    permission = new ThinkgatePermission(permissionID);
                    permission.PermissionLevel = (ThinkgatePermission.PermissionLevelValues)Enum.ToObject(typeof(ThinkgatePermission.PermissionLevelValues), permissionLevel);

                    _user.addPermission(permission);
                }
            }
            else
            {
                _user.removePermission(permission);
                lblResultMessage.Text = string.Format("Permission {0} was revoked from user {1}.", permissionName, _user.UserName);
                foreach (TreeListDataItem item in editedItem.ChildItems)
                {
                    Guid.TryParse(item.GetDataKeyValue("PermissionId").ToString(), out permissionID);
                    if (permissionID == default(Guid)) return;
                    permission = new ThinkgatePermission(permissionID);

                    _user.removePermission(permission);
                }
            }
            sessionObject.UpdateUserObject(_user);
        }
        protected void rgUserSchoolsForEditing_UpdateCommand(Object source, GridCommandEventArgs e)
        {
            SessionObject sessionObject = (SessionObject)Session["SessionObject"];
            _user = sessionObject.SelectedUser;
            if (_user == null) return;

            var editedItem = e.Item as GridEditableItem;
            if (editedItem == null) return;

            //Get the new values:
            var newValues = new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
            int? schoolID = DataIntegrity.ConvertToNullableInt(editedItem.GetDataKeyValue("ID").ToString());
            if (schoolID == null) return;

            bool hasSchool = newValues["HasSchool"] != null && DataIntegrity.ConvertToBool(newValues["HasSchool"]);

            ThinkgateSchool school = new ThinkgateSchool((int)schoolID, false);

            if (hasSchool)
            {
                _user.addSchool(school);
                lblResultMessage.Text = string.Format("User {0} was added to school {1}.", _user.UserName, school.Name);
            }
            else
            {
                _user.removeSchool(school);
                lblResultMessage.Text = string.Format("User {0} was removed from school {1}.", _user.UserName, school.Name);
            }
            sessionObject.UpdateUserObject(_user);

        }
        protected void rgUserRolesForEditing_UpdateCommand(Object source, GridCommandEventArgs e)
        {
            var editedItem = e.Item as GridEditableItem;
            if (editedItem == null) return;

            //Get the new values:
            Hashtable newValues = new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
            string roleName = newValues["RoleName"] == null ? string.Empty : newValues["RoleName"].ToString();
            bool hasRole = newValues["HasRole"] != null && DataIntegrity.ConvertToBool(newValues["HasRole"]);
            if (String.IsNullOrEmpty(roleName)) return;

            SessionObject sessionObject = (SessionObject)Session["SessionObject"];
            _user = sessionObject.SelectedUser;
            if (hasRole)
            {
                Roles.AddUserToRole(_user.UserName, roleName);
                _user.Roles.Add(new ThinkgateRole(roleName));
                lblResultMessage.Text = string.Format("User {0} was added to role {1}.", _user.UserName, roleName);
            }
            else
            {
                Roles.RemoveUserFromRole(_user.UserName, roleName);
                _user.Roles.Remove(new ThinkgateRole(roleName));
                lblResultMessage.Text = string.Format("User {0} was removed from role {1}.", _user.UserName, roleName);
            }

            sessionObject.UpdateUserObject(_user);
        }