flex_security使用

来源:互联网 发布:网络暴力的影响 编辑:程序博客网 时间:2024/06/04 18:58

flex-security的使用也很简单,首先引入flex_security.swflib包,在进行权限控制之前通过SecurityControler的静态方法start开启权限控制。如我在主程序的创建后通过下面的语句加入开启权限控制:

 

var parem:ArrayCollection = new ArrayCollection();

parem.addItem("user");

SecurityControler.start(parem, "visible");

 

SecurityControler是进行权限控制的主要类,共包括七个静态方法,如下:

 

/**
* 启动权限控制
* permissions: 字符串列表,为拥有的权限
* default_control_by: 按那种方式控制权限,可选值: visible,enabled,remove,includeInLayout
*/
public static function start(permissions:ArrayCollection = null,default_control_by : String = "visible"):void 
 
//停止权限控制
public static function stop():void 
 
// 移除所有权限
public static function removeAllPerms():void
/**
* 更新拥有的权限
* perms: 字符串列表,用户拥有的权限
*/
public static function updatePerms(perms:ArrayCollection):void 
 
//增加一条权限
public static function addPerm(permName:String):void
 
// 减少一条权限
public static function removePerm(permName:String):void
 
// 判断是否拥有权限
//示例: if(SecurityControler.isPermitted('blog_delete')) { do some thing}
public static function isPermitted(perm:String):boolean
 

其中start方法用户开启权限控制,controlBy即为组件不具有权限时要进行的操作,包括5种操作,分别是visible,enabled,remove,includeInLayout,分别是:

 

visible : 可见性,有权限为true,没有权限为false

enabled : 激活状态,有权限为true,没有权限为false

includeInLayout : 有权限为true,没有权限为false

remove : 使用removeChild()将对象从parentremove掉,有权限不remove,没有权限则remove

all : 包含前面所讲的:visible,enabled,includeInLayout,但不包含remove

现在就可以对要进行控制的组件进行权限控制了,可以通过以下几种方法:

1.    注解的方式,即设置metadata

[Protected(permission="user",id="userP",controlBy="enabled")]

上面的方法说明我对iduserP的组件进行权限控制,如果其不具有user的权限,就通过enabled的方式进行处理,即将其设为不可用,使用metadata的方式进行权限控制可以使一个组件同时具有两个以上不同权限,如下:

[Protected(permission="admin,customer",id="customerP",controlBy="enabled")]

上面的语句说明,customerP同时具有admincustomer的权限,不管用户拥有哪一种权限改组件都将可用,否则不可用。

2.    通过styleName的方式进行设置

<mx:Button label="new user" styleName="security(user_new,enabled)" />

则是如果用户拥有权限user_new,则可以使用该组件,否则变为不可用。styleName里的写法是security(perm, controlBy),两个参数,前一个是权限,后一个是控制方式,两个参数都可省略,如果权限省略则以其ID代替,后者省略则以开启权限时的默认方式进行处理。

这里他获取权限和控制方式是通过正则表达式进行分割的,所使用的正则表达式为 “/security\((\w*),?(\w*)\)?/”,可以看出通过括号内的逗号分割成两个参数,一个为权限一个为控制方式,因此不能为一个组件设置多个权限,但如果需要的话可以修改其源码以实现此功能,但是由于在实际做程序时此属性有可能有其他用途,因此并不推荐使用styleName的方式进行权限控制。

3.    通过actionscript代码进行手动配置

可以在通过在actionscript代码中手动的为组件增加权限控制,如下

 
SecurityControler.start(null,'enabled');
//增加需要权限保护的UI资源
SecurityControler.addSecurityAction(user_new);

上面即说开启权限控制,默认使用enabled的方式控制,并且增加了IDuser_new的组件进行权限控制,addSecrityAction共有三个参数,如下:

/* 增加需要保护的资源*/
           public static function addSecurityAction( comp: UIComponent, permission:String=null, controlBy : String = null) : void {
                                                         ……

}

可以看出,第一个参数是必须的,为要进行控制的组件,第二个参数为权限名,可省略,如果为空则以UIID的权限名,第三个参数为控制方式,可省略,为空则使用默认。然后就可以动态的增加减少用户权限进行UI控制了。

4.通过实现接口的方式进行控制

最后一种方法,可以通过实现接口的方式进行权限控制,如下:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

               implements="cn.org.rapid_framework.flex_security.ISecurityMetadata"
               creationComplete="creationComplete()" xmlns:local="*">
 
        <mx:Script>
               <![CDATA[
                            import cn.org.rapid_framework.flex_security.SecurityAction;
                           import mx.collections.ArrayCollection;
                           import cn.org.rapid_framework.flex_security.SecurityControler;
                           import cn.org.rapid_framework.flex_security.ISecurityMetadata;
 
                           // 实现ISecurityMetadata接口并返回需要权限保护的UI组件
                         public function getSecurityActions() : Array{
                               return [
                               new SecurityAction(user_new),
                               new SecurityAction(user_delete),
                               new SecurityAction(user_update),
                               new SecurityAction(user_search)
                               ];
                         }
                         public function creationComplete():void {
                               //启动权限控制
                               SecurityControler.start();
                        }
                     private var permissionList : Array = ['user_new', 'user_delete', 'user_update', 'user_search', 'passwordInput'];
                   ]]>
               </mx:Script>
<mx:HBox>
<mx:Label text="右键可以查看源代码:Permission: " fontSize="15" />
<mx:ComboBox id="permInput" dataProvider="{permissionList}"/>
<mx:Button click="SecurityControler.addPerm(permInput.selectedLabel)" label="Add" />
<mx:Button click="SecurityControler.removePerm(permInput.selectedLabel)" label="Remove" />
<mx:Button click="SecurityControler.removeAllPerms()" label="Clean All" />
<mx:Button click="SecurityControler.addAllPerms(new ArrayCollection(permissionList))" label="Add All" />
</mx:HBox>
               <!-- 被权限控制的按钮,没有权限将看不见下面的相关按钮 -->
               <mx:HBox>
                               <mx:Button label="new user" id="user_new" />
                               <mx:Button label="delete user"  id="user_delete" />
                               <mx:Button label="update user" id="user_update" />
                               <mx:Button label="search user"  id="user_search" />
                               <mx:TextArea id="passwordInput"/>
               </mx:HBox>
</mx:Application>