CakePHP ACL和Auth不起作用(CakePHP ACL and Auth not working)
我有一些关于CakePHPs ACL和Auth系统的问题。
- 什么是acos表中别名的命名约定? 是'控制器/帖子/添加'还是'添加'与'帖子'的父母ID?
- 我的登录无效。 当我尝试登录时,我没有收到任何错误消息,但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.
- 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'?
- 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
最满意答案
你的直觉很好 - 你不想重复自己 ,并且有更好的方法来构造这段代码。 但是,不应该共享变量 ,而应该考虑松散连接的小块 。 编写能很好地完成一件事的方法,并将它们组合在一起。 例如,我们可以编写一个
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 aclient
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 yourAnalytic
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 returnget_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 local variable is undefined)[2024-03-17]
在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
-
Ruby on Rails的未定义局部变量或方法`profile'(Undefined local variable or method `profile' for Ruby on Rails)[2022-01-02]
您需要将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 ...