我在制作中间件时遇到了麻烦,该中间件检查用户是否拥有所请求的资源。
例如,如果用户转到/playlists/1/edit,而他们不拥有播放列表1,则应该显示401错误。
这是我到目前为止的内容:
class CheckOwnership {
public function handle(Request $request, Closure $next)
{
if (Playlist::find($request->route()->parameters()['playlists'])->user_id !== $request->user()->id)
{
return response('Unauthorized.', 401);
}
return $next($request);
}
}
Run Code Online (Sandbox Code Playgroud)
这很糟糕,仅适用于播放列表资源,但我找不到更好的方法。
谢谢
我们的网站目前正在使用"Yoast的WordPress SEO",
rel="next"并且rel="prev"在类别和存档页面上运行良好,但是在我们创建的页面模板中, rel="next"并rel="prev"没有显示.(此页面模板也有分页)
我们的网站结构=>我们有" Article"帖子类型
在文章中我们有类别
因为我希望网址 www.sitename.com/loan 没有../category/loan
我创建了名为"贷款"的"页面",并使用page-loan.php页面模板查询帖子类型"文章"类别"贷款"
我想拥有rel="next"并rel="prev"出现在这个页面模板中我也不知道有没有使用Yoast的WordPress SEO来做呢?
或者无论如何修改插件中的下面脚本以制作 rel="next"并rel="prev"出现在页面模板中?
我在插件中找到的脚本
public function adjacent_rel_links() {
// Don't do this for Genesis, as the way Genesis handles homepage functionality is different and causes issues sometimes.
/**
* Filter 'wpseo_genesis_force_adjacent_rel_home' - Allows devs to allow echoing rel="next" / rel="prev" by WP SEO on Genesis installs
* …Run Code Online (Sandbox Code Playgroud) 我正在为猫鼬编写一个中间件,它使用预查询钩子为每个查找对象执行。
postSchema.pre('query', function(query, next) {
// I want to access the req.user object here
query.populate('Category');
next();
});
Run Code Online (Sandbox Code Playgroud)
我想为每个向 api 服务器发出的请求访问 pre 中的 req.user 对象。如何将对象传递给中间件?
甚至有可能吗?
https://github.com/Automattic/mongoose/issues/1931
我找到了上面的但它没有谈论传递 req 对象。
====================
在对这个问题有些困惑后进行编辑。
我想要完成的是获取 req.user 角色和模型名称将其传递给另一个函数以获取查找的查询条件。因此,根据用户角色和访问的模型类型,查询条件会发生变化。
我有两个实体
文章与名为'likesByUsers'的用户有关系
现在,我想通过喜欢的数量订购文章,但是:
我现在拥有的是:
$builder = $this->createQueryBuilder('a');
->select('COUNT(u) AS nbrLikes')
->leftJoin('a.likedByUsers', 'u')
->orderBy('nbrLikes', 'DESC')
->groupBy('a.id')
->getQuery()
->getResult()
;
Run Code Online (Sandbox Code Playgroud)
这正确地返回了喜欢的数量(对于没有喜欢的文章,为0),但它不会返回文章本身
我试过添加
->select('a, COUNT(u) AS HIDDEN nbrLikes')
Run Code Online (Sandbox Code Playgroud)
但它失败了,因为a它不属于GROUP BY
有任何想法吗 ?
我正在尝试使用php进行Google Analytics.我不能使用javascript.这有什么办法吗?
我已经尝试了一些php库,但似乎这些库不起作用.我试过的Lib:http://code.google.com/p/serversidegoogleanalytics/
提前致谢.
我对我的应用程序的POST cURL请求有问题.目前,我正在使用laravel 5构建RESTFUL注册功能.
这个例子的路线是
localhost:8000/user/create
我在终端上使用cURL函数传递值
curl -d 'fname=randy&lname=tan&id_location=1&email=randy@randytan.me&password=randytan&remember_token=Y&created_at=2015-03-03' localhost:8000/auth/register/
Run Code Online (Sandbox Code Playgroud)
这是我的routes.php
Route::post('user/create', 'UserController@create');
这是我存储注册用户的功能
public function create()
{
//function to create user.
$userAccounts = new User;
$userAccounts->fname = Request::get('fname');
$userAccounts->lname = Request::get('lname');
$userAccounts->id_location = Request::get('id_location');
$userAccounts->email = Request::get('email');
$userAccounts->password = Hash::make(Request::get('password'));
$userAccounts->created_at = Request::get('created_at');
$userAccounts->save();
return Response::json(array(
'error' => false,
'user' => $userAccounts->fname . " " . $userAccounts->lname
), 200);
}
Run Code Online (Sandbox Code Playgroud)
执行上面的cURL语法,我收到此错误 TokenMismatchException
你有什么想法?
因为我只在我的几个网址中实现中间件,而且这个cURL注册网址并没有严格执行任何身份验证机制.
谢谢你.
我想在用户登录时在会话中保存一些详细信息。并从与用户Auth()相同的用户表中检索详细信息。登录后是否必须再次查询并设置会话变量,否则auth中是否有办法。提前致谢!:)
题,
我想有一个bash脚本,它将有一个可以从其他bash脚本增加的全局变量.
示例:我有一个如下的脚本:
#! /bin/bash
export Counter=0
for SCRIPT in /Users/<user>/Desktop/*sh
do
$SCRIPT
done
echo $Counter
Run Code Online (Sandbox Code Playgroud)
该脚本将调用文件夹中的所有其他bash脚本,这些脚本将具有以下内容:
if [ "$Output" = "$Check" ]
then
echo "OK"
((Counter++))
Run Code Online (Sandbox Code Playgroud)
我希望它然后递增$ Counter变量,如果它等于"OK",然后将该值传递回初始批处理脚本,这样我就可以保留该计数器编号并在结尾处有一个总计.
关于如何去做的任何想法?