Cakephp 3和身份验证

时间:2016-04-29 08:35:55

标签: authentication cakephp-3.0

在Cakephp 3中有一种简单的方法可以使用角色

APP控制器

public function isAuthorized($user)
{
    // Admin can access every action
    if (isset($user['role']) && $user['role'] === 'admin') {
        return true;
    }

    // Default deny
    return false;
}

POSTS Contoller

public function isAuthorized($user) {
    // All registered users can add posts
    if ($this->action === 'edit') {
        return true;
    }

    return parent::isAuthorized($user);
}

我从http://book.cakephp.org/3.0/en/controllers/components/authentication.html#testing-actions-protected-by-authcomponent知道

$this->auth->deny('add');

正在这样做,但我该如何添加用户/管理员?

1 个答案:

答案 0 :(得分:0)

我使用isAuthorised()方法以非常简单的方式使用了ACL身份验证。我希望它会对你有所帮助。

AppController.php 你必须要定义属性

public class DemoAdapter extends BaseAdapter {
ArrayList<String> list;
Context context;

String TAG = "adapter";

public DemoAdapter(Context context) {

    this.context = context;
    this.list = new ArrayList<>();
    for(int i=0;i<10;i++){
    this.list.add("0");
    }

}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    final Holder holder;
    if (convertView == null) {
        convertView = View.inflate(context, R.layout.total_item_layout, null);
        holder = new Holder(convertView);
        convertView.setTag(holder);
    } else holder = (Holder) convertView.getTag();

    holder.total.setText(list.get(position).toString());
    holder.plusBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int count = Integer.parseInt(list.get(position));
            count = count + 1;
            Log.e(TAG, "onClick: " + count);
            list.set(position, String.valueOf(count));  //update your list like this
            notifyDataSetChanged();
        }
    });
    holder.minusBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int count = Integer.parseInt(list.get(position));
            if (count > 0) {
                count = count - 1;
                list.set(position, String.valueOf(count)); //update your list like this
            } else Toast.makeText(context, "not allowed", Toast.LENGTH_SHORT).show();
            Log.e(TAG, "onClick: " + count);
            notifyDataSetChanged();
        }
    });


    return convertView;
}


static class Holder {
    TextView total;
    Button plusBtn, minusBtn;

    public Holder(View v) {
        total = (TextView) v.findViewById(R.id.total_text_view);
        plusBtn = (Button) v.findViewById(R.id.plus_btn);
        minusBtn = (Button) v.findViewById(R.id.minus_btn);
    }
}
}

定义私有方法

/**
 * ACCESS CONTROL LIST BASED ON METHODS OF CLASS FOR USER ROLES
 */
var $accessControllList = array();

在isAuthorized()中添加以下行。

private function _checkAccessControll() {
    if ($this->Auth->user('id')) {
        if (!isset($this->accessControllList) || empty($this->accessControllList)) {
            return true;
        }

        $action_name = $this->request->params['action'];
        $user_role = $this->Auth->user('role');
        if (isset($this->accessControllList['allowed']) && !empty($this->accessControllList['allowed']) && in_array($action_name, $this->accessControllList['allowed'])) {
            return true;
        } else if (isset($this->accessControllList['role_base'][$user_role]) && !empty($this->accessControllList['role_base'][$user_role]) && in_array($action_name, $this->accessControllList['role_base'][$user_role])) {
            return true;
        }

        throw new \Cake\Network\Exception\ForbiddenException(__('You not have access for this page'));
    }
    return true;
}

在任何控制器中,您都需要使用角色映射ACL。对于PostsController.php文件,如下所示

$this->_checkAccessControll();