Laravel - SuperAdmin can't see other roles - laravel

I added this in my User model:
private $rank;
public function isSuperAdmin(): bool {
if ($this->rank >= 3) {
return true;
}
return false;
}
public function isAdmin(): bool
{
if ($this->rank == 2 || $this->isSuperAdmin()) {
return true;
}
return false;
}
public function isCustomer(): bool {
if ($this->rank == 1|| ($this->isSuperAdmin() || $this->isAdmin())) {
return true;
}
return false;
}
I have rank '3' which equals the isSuperAdmin() function. So when I do the following:
if (Auth::user()->isSuperAdmin())
This works as expected. However when I, as SuperAdmin, try to do the following:
if (Auth::user()->isAdmin())
It doesn't work at all, Laravel doesn't display whatever is between #if(Auth::user()->isAdmin()) in my views when I have the role SuperAdmin. This is the same for all the roles and I don't understand why.. what am I doing wrong?
Ps. I also tried the following in the isAdmin function, which also doesn't work:
if ($this->rank >= 2 || $this->isSuperAdmin()) {

After I removed private $rank everything worked as expected.

Related

Cannot validate textfield using codeigniter validation using callback

I want to validate from and to text fields using codeigniter validation. I have created
validateSchedule function that will validate on callback but here validation is not
working it is working only for required condition.
public function validateSchedule()
{
$fromDate=$_POST['from_date'];
$toDate=$_POST['toDate'];
if(empty($toDate) || empty($fromDate))
{
return TRUE;
}
else
{
$diffNoof_days = 10;
if(strtotime($fromDate) > strtotime($toDate)){
$this->form_validation->set_message('validateSchedule','from_date_must_be_smaller_than_to_date');
return FALSE;
}else if(strtotime($fromDate) == strtotime($toDate)){
$this->form_validation->set_message('validateSchedule','from_date_to_must_not_be_same');
return FALSE;
}else if($diffNoof_days>10)
{
$this->form_validation->set_message('validateSchedule','duration_should_not_exceed_10_days');
return FALSE;
}
}
}
$this->form_validation->set_rules('from_date','From Date','trim|required');
$this->form_validation->set_rules('to_date','To Date','trim|required|callback_validateSchedule');
You don't show the actual callback, so I'm speculating that you have named the method wrong by not removing the callback_ prefix. In other words, the definition
public callback_validateSchedule($str)
{
...
}
should be
public validateSchedule($str)
{
...
}
If I have guessed wrong please show the actual code for validateSchedule()

Object of class Illuminate\Support\Collection could not be converted to int

I have condition in Controller with checks the value in database and based on this value is redirecting user to different login pages
public function getLogin()
{
$login = DB::table('login')->pluck('login');
//var_dump($login);
if ($login == 1) {
return View::make('users.login');
} else {
return View::make('users.login1');
}
}
When I go to login page I've got this error
Object of class Illuminate\Support\Collection could not be converted to int
When I var_dump($login); I get
object(Illuminate\Support\Collection)#304 (1) { ["items":protected]=> array(1) { [0]=> int(1) } }
How can I fix this error?
You can use it like this :
public function getLogin()
{
$login = DB::table('login')->pluck('login');
//var_dump($login);
if ($login->count() == 1) {
return View::make('users.login');
} else {
return View::make('users.login1');
}
}
$login is a collection, you get all the values of table login with your query. if you want this create a for loop and have your if statement inside.
for example :
foreach ($login as $val) {
if ($val== 1) {
return View::make('users.login');
} else {
return View::make('users.login1');
}
}
You should use isEmpty() or count() here, for example:
if (!$login->isEmpty())
if (count($login) > 0)
if ($login->count() > 0)
Ok Its just simple in this case, You can use [0] with login to access it as int.
public function getLogin()
{
$login = DB::table('login')->pluck('login');
//var_dump($login);
if ($login[0] == 1) {
return View::make('users.login');
} else {
return View::make('users.login1');
}
}

Telerik RadPageview Right-to-Left Layout Arrow Keys

I have a RadPageview in Strip Mode and I want my program to had support a right-to-left language. When I changing the Right-to-Left Property to true it works fine. but when I run my program and use Arrow Keys (Left or Right) it doesn't work correctly. Left key goes to Right and Right key goes to Left. How can I fix it?
Telerik answered my question:
http://www.telerik.com/forums/right-to-left-arrow-keys-problem
the possible solution that I can suggest is to create a custom RadPageViewStripElement and override its IsNextKey and IsPreviousKey methods on a way to reverse the previous/next navigation as follows:
public class CustomPageView : RadPageView
{
public override string ThemeClassName
{
get
{
return typeof(RadPageView).FullName;
}
}
protected override RadPageViewElement CreateUI()
{
switch (this.ViewMode)
{
case PageViewMode.Strip:
return new CustomRadPageViewStripElement();
default:
return base.CreateUI();
}
}
}
public class CustomRadPageViewStripElement : RadPageViewStripElement
{
public CustomRadPageViewStripElement()
{
}
protected override Type ThemeEffectiveType
{
get
{
return typeof(RadPageViewStripElement);
}
}
protected override bool IsNextKey(Keys key)
{
if (this.RightToLeft)
{
if (key == Keys.Left)
{
return true;
}
else
{
return false;
}
}
return base.IsNextKey(key);
}
protected override bool IsPreviousKey(Keys key)
{
if (this.RightToLeft)
{
if (key == Keys.Right)
{
return true;
}
else
{
return false;
}
}
return base.IsPreviousKey(key);
}
}
I fix it programmatically by using ProcessCmdKey of form.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Right))
{
int x = radPageView1.Pages.IndexOf(this.radPageView1.SelectedPage) - 1;
if (x < 0)
x = radPageView1.Pages.Count() - 1;
else if (x >= radPageView1.Pages.Count())
x = 0;
radPageView1.SelectedPage = radPageView1.Pages[x];
return true;
}
else if(keyData == Keys.Left)
{
int x = radPageView1.Pages.IndexOf(this.radPageView1.SelectedPage) + 1;
if (x <= 0)
x = radPageView1.Pages.Count() - 1;
else if (x >= radPageView1.Pages.Count())
x = 0;
radPageView1.SelectedPage = radPageView1.Pages[x];
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}

MVC3 AuthorizeAttribute

This allows "frankl" to access but blocks the admins. What have I done wrong?
[Authorize(Order=1,Roles = "Admin",Users="frankl")]
public class AuthorizeBaseController_Admins_frank : Controller
{
}
It is probably simple but I don't see any examples that combine the two and the "Allowmultiple" property generates an error when I try to add it.
Thanks,
Chris
Roles and Users should be used exclusively. If you want to combine them you could write a custom authorize attribute:
public class MyAuthoirizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException("httpContext");
}
var user = httpContext.User;
if (!user.Identity.IsAuthenticated)
{
return false;
}
var usersSplit = SplitString(Users);
var rolesSplit = SplitString(Roles);
return
(usersSplit.Length > 0 && usersSplit.Contains(user.Identity.Name, StringComparer.OrdinalIgnoreCase)) ||
(rolesSplit.Length > 0 && rolesSplit.Any(user.IsInRole));
}
private string[] SplitString(string original)
{
if (string.IsNullOrEmpty(original))
{
return new string[0];
}
return (from piece in original.Split(',')
let trimmed = piece.Trim()
where !string.IsNullOrEmpty(trimmed)
select trimmed).ToArray();
}
}
and then:
[MyAuthorize(Order = 1, Roles = "Admin", Users="frankl")]
public class AuthorizeBaseController_Admins_frank : Controller
{
...
}
Unfortunately the AuthorizeAttribrute will let you either specify valid users, or valid roles - not both. Here is the relevant bit of code from the MVC 3 source.
protected virtual bool AuthorizeCore(HttpContextBase httpContext) {
if (httpContext == null) {
throw new ArgumentNullException("httpContext");
}
IPrincipal user = httpContext.User;
if (!user.Identity.IsAuthenticated) {
return false;
}
if (_usersSplit.Length > 0 && !_usersSplit.Contains(user.Identity.Name, StringComparer.OrdinalIgnoreCase)) {
return false;
}
if (_rolesSplit.Length > 0 && !_rolesSplit.Any(user.IsInRole)) {
return false;
}
return true;
}
You will either need to make 'frankl' an Admin, or create a custom authorization attribrute

CodeIgniter and friendly urls

I currently have a set of urls which are derived from their controller names but they're not very url friendly.
For example, is there anyway I change:
example.com/admin/news_manager/add_article
to
example.com/admin/news-manager/add-article
Any help would be greatly appreciated! Thank you.
If its just that one function you can open applications/config/routes.php and add a line something like this:
$route['^admin/news-manager/add-article'] = $route['admin/news_manager/add_article'];
depending on what your other urls are you could come up with a more generic rule.
Here's what I have, put this in your application/core folder as MY_Router.php
It will convert all of the -'s in your url to _'s.
<?php
class MY_Router extends CI_Router {
function set_class($class)
{
$this->class = $this->replace_underscores($class);
}
function set_method($method)
{
$this->method = $this->replace_underscores($method);
}
private function replace_underscores($string){
return str_replace('-', '_', $string);
}
function _validate_request($segments)
{
$this->segments = $segments;
if(empty($this->segments) || $this->controller_is_in_root_folder())
return $this->segments;
if ($this->controller_is_in_a_subfolder())
{
$this->segments = $this->set_directory_and_remove_from_array();
if ($this->at_least_one_segment() > 0 && !$this->default_controller_is_in_subfolder())
{
show_404($this->fetch_directory().$this->segments[0]);
}
else
{
$this->set_class($this->default_controller);
$this->set_method('index');
if (!$this->default_controller_is_in_subfolder())
{
$this->directory = '';
return array();
}
}
return $this->segments;
}
else
show_404($this->segments[0]);
}
private function at_least_one_segment(){
return count($this->segments) >= 1;
}
private function controller_is_in_a_subfolder(){
return is_dir(APPPATH.'controllers/'.$this->segments[0]);
}
private function controller_is_in_root_folder(){
return file_exists(APPPATH.'controllers/'.str_replace('-', '_', $this->segments[0]).EXT);
}
private function set_directory_and_remove_from_array(){
$this->set_directory($this->segments[0]);
return array_slice($this->segments, 1);
}
private function default_controller_is_in_subfolder(){
return file_exists(APPPATH.'controllers/'.$this->fetch_directory().str_replace('-', '_', $this->segments[0]).EXT);
}
}
You can use URI routing. See the URI routing page in the CodeIgniter docs.

Resources