Hızlıca şunu yazdım. Bu mantıkla istediğiniz gibi filtreleme yapabilirsiniz. Ben grouplara göre deneme yaptım.
Kullandığım algoritma mantığıyla 2 ya da 3 methodla filtreleme yapan public bir methodu User modeline eklenebilir. Statik çağrılarak verilen grouba göre sonuç alınabilir.
Performans sorunu olursa caching yapılır.
Yaptığım teste öncelikle 100 tane kullanıcı yarattım. Sonra 2 tane group yarattım. Teneke ve gold isimde gruplar bunlar.
100 kullanıcının 50 tanesini gold gruba ekledim. Diğerlerini ise diğer gruba. Assertler bu durumu test ettim...
iki adet collection objesi oluşturdum. Torba gibi..
Sonra Mevcut grup isimlerine göre kullanıcıları ayıklayıp ait oldukları collection nesnelerine ekledim.. Altınsan şuraya , tenekeysen buraya gibi..
En sonunda iki adet torbam oldu.
Buna benzer mantıkla User modeli üzerinden static olarak çağırılacak bir method ve yardımcı private methodlarla filtrelemeler yapılabilir. Senty Clası da extend edilebilir ve benzer mantıkla çalışan methodlar eklenilebilir ve extend edilmiş class kullanılabilir.
public function testSentryAccessFilterSimpe() {
// creating gold groups for test
$groupGold = \Sentry::createGroup(array(
'name' => 'Gold',
'permissions' => array(
'foo' => 1,
'bar' => 1,
),
));
// creating teneke groups for test
$groupTeneke = \Sentry::createGroup(array(
'name' => 'teneke',
'permissions' => array(
'foo' => 1,
'bar' => 1,
),
));
// creating 100 users
for($i = 0 ; $i < 100 ; $i++) {
Sentry::createUser(array(
'email' => str_random(5) . '@example.com',
'password' => 'test',
'activated' => true,
));
}
$allusers = \Sentry::findAllUsers();
// let's create 100 users
$this->assertEquals(100, count($allusers)); //passed !
// now we will create 50 members join gold group and
// other member will to teneke group..
$counter = 0;
foreach ($allusers as $v) {
switch ($counter < 50) {
case true : $v->addGroup($groupGold);
break;
default : $v->addGroup($groupTeneke);
}
$counter++;
}
// Now We have 50 members in gold group and 53 numbers in teneke group
// we will add members is in tenekeler group to this collection
$tenekeler = new \Illuminate\Database\Eloquent\Collection();
// we will add members is in golds group to this collection
$golds = new \Illuminate\Database\Eloquent\Collection();
// let's pick members up according to own group
foreach ($allusers as $v) {
if ( $v->inGroup($groupTeneke)) {
$tenekeler->add($v);
continue;
}
$golds->add($v);
}
// Now we must have 50 gold member and 50 teneke member
$this->assertEquals(50, count($golds)); // passed !
$this->assertEquals(50, count($tenekeler)); // passed !
}
User Model üzinden daha kısa versiyonu..
public function testSentryAccessFilterSimpe() {
// creating gold groups for test
$groupGold = \Sentry::createGroup(array(
'name' => 'Gold',
'permissions' => array(
'foo' => 1,
'bar' => 1,
),
));
// creating teneke groups for test
$groupTeneke = \Sentry::createGroup(array(
'name' => 'teneke',
'permissions' => array(
'foo' => 1,
'bar' => 1,
),
));
// creating 100 users
for($i = 0 ; $i < 100 ; $i++) {
Sentry::createUser(array(
'email' => str_random(5) . '@example.com',
'password' => 'test',
'activated' => true,
));
}
$allusers = \Sentry::findAllUsers();
// let's create 100 users
$this->assertEquals(100, count($allusers)); //passed !
// now we will create 50 members join gold group and
// other member will to teneke group..
$counter = 0;
foreach ($allusers as $v) {
switch ($counter < 50) {
case true : $v->addGroup($groupGold);
break;
default : $v->addGroup($groupTeneke);
}
$counter++;
}
// Now We have 50 members in gold group and 53 numbers in teneke group
// getting 50 user which is member of Gold Group by collection filter method
$test = User::all(array('id'))->filter(function($user) use ($groupGold) {
return \Sentry::findUserById($user->id)->inGroup($groupGold);
});
$this->assertEquals(50, count($test)); //passed
}