首页 \ 问答 \ CakePHP ACL和Auth不起作用(CakePHP ACL and Auth not working)

CakePHP ACL和Auth不起作用(CakePHP ACL and Auth not working)

我有一些关于CakePHPs ACL和Auth系统的问题。

  1. 什么是acos表中别名的命名约定? 是'控制器/帖子/添加'还是'添加'与'帖子'的父母ID?
  2. 我的登录无效。 当我尝试登录时,我没有收到任何错误消息,但Auth.User和Auth-> user('id')都是空的。 可能是什么问题呢? 我会像CakePHP的ACL示例中那样做所有事情。

谢谢。

更新:我现在的登录工作(问题与服务器上的时间设置),我的acos表看起来是正确的,但我不能去任何行动。 我允许aros_acos表中的操作。 如果我使用'$ this-> Acl-> check()'检查权限,如果我给出动作的单个名称而不是完整路径('controllers / Posts / add'不起作用),它就会起作用。


I have a few questions about CakePHPs ACL and Auth system.

  1. Whats the naming convention of the aliases in the acos table? Is it 'controllers/Posts/add' or just 'add' with the parent id from 'Posts'?
  2. My login doesn't work. When I try to login I don't get any error message but the Auth.User and the Auth->user('id') are both empty. What could be the problem? I do everything like in the ACL example from CakePHP.

Thank you.

UPDATE: My login works now (Problem with the time settings on the server) and my acos table looks right but I can't go to any action. I allowed the action in the aros_acos table. If I check the permission with '$this->Acl->check()' it just works if I give the single name of the action and not the full path ('controllers/Posts/add' doesn't work).


原文:https://stackoverflow.com/questions/12446829
更新时间:2023-04-07 11:04

最满意答案

你的直觉很好 - 你不想重复自己 ,并且有更好的方法来构造这段代码。 但是,不应该共享变量 ,而应该考虑松散连接的小块 。 编写能很好地完成一件事的方法,并将它们组合在一起。 例如,我们可以编写一个get_client方法,该方法只返回client以供其他方法使用:

protected
def get_client
  client  = Google::APIClient.new(
    application_name: "Example Application",
    application_version: "1.0")

  client.authorization = Signet::OAuth2::Client.new(
    :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
    :audience             => 'https://accounts.google.com/o/oauth2/token',
    :scope                => 'https://www.googleapis.com/auth/analytics.readonly',
    :issuer               => SERVICE_ACCOUNT_EMAIL_ADDRESS,
    :signing_key          => Google::APIClient::KeyUtils.load_from_pkcs12(PATH_TO_KEY_FILE, 'notasecret')).tap { |auth| auth.fetch_access_token! }
  client
end

protected因为外部代码 - Analytic类之外的东西 - 不应该直接使用它。 他们应该使用我们为他们提供的方法。


您还可以提供一个帮助方法来从API获取结果。 我不熟悉查询API,但看起来它是您的metrics值发生变化。 也许是这样的:

protected
def get_result(metrics)
  client = self.get_client
  api_method = client.discovered_api('analytics','v3').data.ga.get

  result = client.execute(:api_method => api_method, :parameters => {
    'ids'        => PROFILE,
    'start-date' => Date.new(2014,1,1).to_s,
    'end-date'   => Date.today.to_s,
    'dimensions' => 'ga:pagePath',
    'metrics'    => metrics,
    'filters'    => 'ga:pagePath==/'
   })
  result
end

现在您可以编写外部类可以使用的简单方法:

def new_users
  get_result('ga:newUsers')
end

def total_visits
  get_result('ga:pageViews')
end

如果可以,尝试从这些方法返回简单数据。 也许total_visits会返回get_result('ga:pageViews')['totalsForAllResults']['ga:pageviews'] 。 您班外的代码不应该知道使用它的GA数据格式。 这称为封装


Your instincts are good - you don't want to repeat yourself, and there are better ways of structuring this code. But rather than sharing variables, you should think about small pieces, loosely joined. Write methods that do one thing well, and combine them together. For instance, we could write a get_client method that just returns a client for other methods to use:

protected
def get_client
  client  = Google::APIClient.new(
    application_name: "Example Application",
    application_version: "1.0")

  client.authorization = Signet::OAuth2::Client.new(
    :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
    :audience             => 'https://accounts.google.com/o/oauth2/token',
    :scope                => 'https://www.googleapis.com/auth/analytics.readonly',
    :issuer               => SERVICE_ACCOUNT_EMAIL_ADDRESS,
    :signing_key          => Google::APIClient::KeyUtils.load_from_pkcs12(PATH_TO_KEY_FILE, 'notasecret')).tap { |auth| auth.fetch_access_token! }
  client
end

It's protected because external code - stuff outside your Analytic class - shouldn't work with it directly. They should use the methods we provide for them.


You could also provide a helper method for getting results from the API. I'm not familiar with the query API, but it looks like it's your metrics value that changes. So maybe something like this:

protected
def get_result(metrics)
  client = self.get_client
  api_method = client.discovered_api('analytics','v3').data.ga.get

  result = client.execute(:api_method => api_method, :parameters => {
    'ids'        => PROFILE,
    'start-date' => Date.new(2014,1,1).to_s,
    'end-date'   => Date.today.to_s,
    'dimensions' => 'ga:pagePath',
    'metrics'    => metrics,
    'filters'    => 'ga:pagePath==/'
   })
  result
end

Now you can write simple methods that your external classes can use:

def new_users
  get_result('ga:newUsers')
end

def total_visits
  get_result('ga:pageViews')
end

If you can, try to return simple data from these methods. Maybe total_visits is going to return get_result('ga:pageViews')['totalsForAllResults']['ga:pageviews'] instead. Code outside your class shouldn't have to know about the GA data format to work with it. This is called encapsulation.

相关问答

更多
  • 在Ruby中,局部变量只能在定义的范围内访问。 每当你进入/离开一个类,一个模块或一个方法定义你的范围在Ruby中改变。 例如 : v1 = 1 class MyClass # SCOPE GATE: entering class v2 = 2 local_variables # => ["v2"] def my_method # SCOPE GATE: entering def v3 = 3 local_variables # => ["v3"] end # SCOP ...
  • ff在test方法定义中不可访问的原因很简单,就是方法(使用def关键字创建)创建一个新的范围。 与分别使用class和module关键字定义类和模块一样。 在这种情况下, main (顶层对象)的作用几乎完全与范围问题无关。 请注意,如果您希望您的test方法可以访问定义上下文中定义的locals,那么请使用define_method (或者您的情况, define_singleton_method方法),请参阅此处: ff = "hi" define_singleton_method("test") { ...
  • 错过s或者也许不应该有一个。 order = meal.create_order(order_params) respond_with order Missing an s or perhaps there shouldn't be one. order = meal.create_order(order_params) respond_with order
  • 您需要将def profile为辅助方法,因此可以使用此方法进行查看。 只需在def profile方法下面添加以下行: helper_method :profile 你的最后一堂课将如下所示: class StaticPagesController < ApplicationController def profile redirect_to profile_path(current_user) end helper_method :profile en ...
  • 尝试 puts "Name?, eg. Willow Rosenberg" name = gets.chomp number = rand(1000..9000) + 1 data = [ { name: name, number: number, email: name.split(' ').last + number.to_s[-3, 3] + "@btvs.com" } ] puts data Try puts "Name?, eg. Willow Rosenberg ...
  • 你的直觉很好 - 你不想重复自己 ,并且有更好的方法来构造这段代码。 但是,不应该共享变量 ,而应该考虑松散连接的小块 。 编写能很好地完成一件事的方法,并将它们组合在一起。 例如,我们可以编写一个get_client方法,该方法只返回client以供其他方法使用: protected def get_client client = Google::APIClient.new( application_name: "Example Application", application_v ...
  • 我认为局部变量一旦被阐明就被声明。 在ruby中,查找首先查找局部变量,如果它存在则使用它,如果不存在则查找方法。 这意味着val = val将第一个val声明为local,然后左手val将其匹配(不确定它我应该检查显微镜下的红宝石以确定) 如果你试试 class A def val 10 end def test back = [] x = val back << x val = x + 1 back << val x = val ...
  • 一旦Ruby确定该name是变量而不是方法调用,该信息适用于它所在的范围的整体。 在这种情况下,它将其视为整个方法。 问题是如果你有一个方法和一个具有相同名称的变量,那么变量似乎只能在变量可能被分配到的行上保留,并且这种重新解释会影响该方法中的所有后续行。 与通过某种前缀,后缀或其他指示符明确方法调用的其他语言不同,在Ruby name ,变量和name方法调用在代码中看起来相同,唯一的区别在于它们在“编译”时的解释方式时间优于执行。 所以这里发生的事情有点令人困惑和微妙,但你可以看到如何用local_va ...
  • 基于你在上次测试中得到的最后一个错误,我建议你运行bundle exec rake routes并检查你是否正确定义了路径,就像它在教程上一样(你可以复制粘贴在这里,这样我们就可以检查一切是否正常)。 验证看起来还不错。 您还可以判断此错误是偶然发生,只发生一次或每次? 你重启Ruby是什么意思? 编辑。 检查routes.rb中路由的定义,如果这是正确的,请get 'static_pages/help' 。 您可以尝试将其更改为get 'static_pages/help', as: 'static_pa ...
  • 您将变量hey作为参数传递给方法wri() 。 你可能想要字符串'hey' >def wri(var) > puts var >end >nil >wri('hey') hey => nil >the_variable_hey = 'hey' => 'hey' >wri(the_variable_hey) hey => nil You are passing the variable hey as an argument to the method wri(). You probably want th ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)