FuelPHP SimpleAuth custom column name? - fuelphp

With SimpleAuth package, Im working on login function.
By default, user table is supposed to have a column "username".
But my system has a user table with "login_id" instead of "username".
Fuel\Core\Database_Exception [ 1054 ]:
Unknown column 'username' in 'where clause'
[ SELECT * FROM `m_user` WHERE (`username` = 'admin' OR `email` = 'admin')
AND `password` = 'mIIMXQgANGZ21XHRTpI2Krpla9yx3UwEw0PCNfjAN4I=' ]
I cannot figure out how it handles custom field name.
I tried configuration on APPPATH/config/simpleauth.php with "'username_post_key' => 'login_id'," which did not work..
Thanks in advance.
### APPPATH/config/auth.php ###
return array(
'driver' => 'SimpleAuth',
'verify_multiple_logins' => false,
'salt' => 'techtech',
'iterations' => 10000,
);
### APPPATH/config/simpleauth.php ###
<?php
return array(
'db_connection' => null,
'table_name' => 'm_user',
'table_columns' => array('*'),
'guest_login' => true,
'multiple_logins' => false,
'remember_me' => array(
'enabled' => false,
'cookie_name' => 'rmcookie',
'expiration' => 86400 * 31,
),
'groups' => array(
),
'roles' => array(
),
'login_hash_salt' => 'tech_login',
'username_post_key' => 'login_id',
'password_post_key' => 'password',
);
### user table ###
+----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| login_id | varchar(100) | NO | | NULL | |
| first_name | varchar(20) | NO | | NULL | |
| last_name | varchar(20) | NO | | NULL | |
| password | varchar(255) | NO | | NULL | |
| last_login_at | timestamp | YES | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
| deleted_at | timestamp | YES | | NULL | |
+----------------+------------------+------+-----+---------+----------------+

You can't configure it, because username_post_key is for POST field name, not for table column name. There is no configuration for table column name.
You must customize SimpleAuth, or create your own driver.

Related

Why can't I use a plsql argument in this where clause?

I have a function below (is_organizer) that works, and lets me use this method as a computed field in Hasura. The function below (is_chapter_member) which is almost identical, doesn't work.
WORKS
CREATE OR REPLACE FUNCTION is_organizer(event_row events, hasura_session json)
RETURNS boolean AS $$
SELECT EXISTS (
SELECT 1
FROM event_organizers o
WHERE
o.user_id::text = hasura_session->>'x-hasura-user-id'
AND
(event_row.id = o.event_id OR event_row.event_template_id = o.event_template_id)
);
$$ LANGUAGE SQL STRICT IMMUTABLE;
BROKEN
CREATE OR REPLACE FUNCTION is_chapter_member(c chapters, hasura_session json)
RETURNS boolean AS $$
SELECT EXISTS (
SELECT 1
FROM chapter_members m
WHERE
m.user_id::text = hasura_session->>'x-hasura-user-id'
AND
c.chapter_id = m.chapter_id
);
$$ LANGUAGE SQL STRICT IMMUTABLE;
When attempting to add this function (not call it, just create it) Postgres gives me the following error:
ERROR: missing FROM-clause entry for table "c"
LINE 9: c.chapter_id = m.chapter_id
Why would a function param need a where clause? Table dumps below...
Table "public.chapters"
Column | Type | Collation | Nullable | Default
-----------------+--------------------------+-----------+----------+--------------------------------------
id | integer | | not null | nextval('chapters_id_seq'::regclass)
title | text | | not null |
slug | text | | not null |
description | jsonb | | |
avatar_url | text | | |
photo_url | text | | |
region | text | | |
maps_api_result | jsonb | | |
lat | numeric(11,8) | | |
lng | numeric(11,8) | | |
created_at | timestamp with time zone | | not null | now()
updated_at | timestamp with time zone | | not null | now()
deleted_at | timestamp with time zone | | |
Table "public.chapter_members"
Column | Type | Collation | Nullable | Default
------------+--------------------------+-----------+----------+---------
user_id | integer | | not null |
chapter_id | integer | | not null |
created_at | timestamp with time zone | | not null | now()
updated_at | timestamp with time zone | | not null | now()
Table "public.events"
Column | Type | Collation | Nullable | Default
-------------------+-----------------------------+-----------+----------+---------------------------------------------------
id | integer | | not null | nextval('events_id_seq'::regclass)
event_template_id | integer | | not null |
venue_id | integer | | |
starts_at | timestamp without time zone | | not null |
duration | interval | | not null |
title | text | | |
slug | text | | |
description | text | | |
photo_url | text | | |
created_at | timestamp without time zone | | not null | now()
updated_at | timestamp without time zone | | not null | now()
deleted_at | timestamp without time zone | | |
ends_at | timestamp without time zone | | | generated always as (starts_at + duration) stored
Table "public.event_organizers"
Column | Type | Collation | Nullable | Default
-------------------+---------+-----------+----------+----------------------------------------------
id | integer | | not null | nextval('event_organizers_id_seq'::regclass)
user_id | integer | | not null |
event_id | integer | | |
event_template_id | integer | | |
This turned out to be using an incorrect column name in the broken function. chapter_id should have just been id on the c argument. I took Richard's prompt and tried putting parens around the arg like (c).chapter_id. This then correctly told me that chapter_id doesn't exist, and allowed me to fix the issue.

Postgres: return true/false if matches found from inner join?

I have implemented a tagging system in my application, using Postgres 9.6. There are three tables.
Projects
Table "public.project"
Column | Type | Collation | Nullable | Default
-------------+-----------------------------+-----------+----------+---------------------------------
id | integer | | not null | nextval('tag_id_seq'::regclass)
name | character varying(255) | | not null |
user_id | integer | | |
Tags
Table "public.tag"
Column | Type | Collation | Nullable | Default
-------------+-----------------------------+-----------+----------+---------------------------------
id | integer | | not null | nextval('tag_id_seq'::regclass)
tag | character varying(255) | | not null |
user_id | integer | | |
is_internal | boolean | | not null | false
Project tags
Column | Type | Collation | Nullable | Default
------------------+-----------------------------+-----------+----------+-----------------------------------------
id | integer | | not null | nextval('project_tag_id_seq'::regclass)
tag_id | integer | | not null |
project_id | integer | | | |
user_id | integer | | not null |
Now I want to get a list of all the projects, annotated with a column that indicates (for a particular tag) whether it has that tag.
So I'd like the results to look like this:
id name has_favorite_tag
1 foo true
2 bar false
3 baz false
This is my query so far:
select project.*, CASE(XXXX) as has_project_tag
from project p
join (select * from project_tag where tag_id=1) pt on p.id=pt.project_id
I know that I want to use CASE to be true when the length of project_tag matches is greater than 0 - but how do I do this?
(In reality the project table has many more fields, of course.)
Here's a possibility (unfiltered for tag_id; add to inner select if necessary):
select project.*, exists(select * from project_tag where id=project.id) as has_project_tag from project;

Postgres group by with aggerate (last comment in a conversation across all conversations)

I want to get the last comment in a conversation between two people.
My table structure as follows:
Table "public.comments"
Column | Type | Modifiers | Storage | Stats target | Description
-------------+-----------------------------+-------------------------------------------------------+----------+--------------+-------------
id | integer | not null default nextval('comments_id_seq'::regclass) | plain | |
body | text | not null | extended | |
target_id | integer | | plain | |
target_type | character varying(255) | | extended | |
created_at | timestamp without time zone | not null | plain | |
updated_at | timestamp without time zone | not null | plain | |
user_id | integer | | plain | |
My Attempt:
SELECT
comments.id,
max(SELECT id comments.created_at),
CASE
WHEN user_id = 1 THEN CONCAT(user_id,'_',target_id)
WHEN target_id = 1 THEN CONCAT(target_id,'_',user_id)
END
FROM comments
WHERE
comments.user_id = 1
OR
(comments.target_type = 'User'
AND
comments.target_id = 1)
GROUP BY
CASE
WHEN user_id = 1 THEN CONCAT(user_id,'_',target_id)
WHEN target_id = 1 THEN CONCAT(target_id,'_',user_id)
END
So I figured out how to group the comments but how to order by created_at and get the latest id and information is where I'm stuck.

Symfon2: How to set default values (options) for Entity field type?

I am creating a form which uses an Entity type.
The entity form type displays Roles as a list of checkboxes.
But I dont know how to set default values. I need to get default values from DB then dynamically check some of those options.
According to the documentation, It seems like 'preferred_choices' option won't do this job.
Can anyone please help me out there?
Sorry about my English if some sentences don't make sense.
3 Tables:
UserRole
+---------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | YES | MUL | NULL | |
| role_id | int(11) | YES | MUL | NULL | |
+---------+---------+------+-----+---------+----------------+
AdminUser
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(25) | NO | UNI | NULL | |
| salt | varchar(32) | NO | | NULL | |
| password | varchar(40) | NO | | NULL | |
| email | varchar(60) | NO | UNI | NULL | |
| is_active | tinyint(1) | NO | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
Role
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | NO | UNI | NULL | |
+-------+-------------+------+-----+---------+----------------+
Form builder:
$builder->add('role', 'entity', array(
'class' => 'AcmeAdminBundle:Role',
'property' => 'name',
'multiple' => TRUE,
'expanded' => TRUE,
));
You must have defined a ManyToMany relation between User and Role, with some traditional methods on User entity : setRoles, getRoles, addRole...
Data that will be loaded in your form are data from a User instance, for example $user.
$user = new User; // or $user is existing User, same logic
$rolesYouWantToSetToUser = array('ROLE_1', 'ROLE_2', 'ROLE_3');
foreach ($rolesYouWantToSetToUser as $roleId) {
// $em must previsouly be set as EntityManager in your code
$role = $em->getReference('YourBundle:Role', $roleId);
$user->addRole($role);
}
// From a controller
$form = $this->createFormBuilder($user)
->add('roles', 'entity', array(
'class' => 'AcmeAdminBundle:Role',
'multiple' => true,
'expanded' => true,
'property' => 'name',
))
->getForm();

Zend_Acl, with roles and permissions stored in database

i want to build an ACL system for my application which have the following requirement.
Users will be assigned single or multiple role. (Admin, Staff) etc.
Role will have permissions.(Send_Invoices, Send_mail, Delete_Invoices, Send_Estimate) etc.
User will be assigned custom permission apart from the role it inherits.
my database structure for ACL is as follows
role:
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| roleName | varchar(50) | NO | UNI | NULL | |
+----------+-------------+------+-----+---------+----------------+
permission:
+----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| permissionName | varchar(50) | NO | | NULL | |
| permissionKey | varchar(50) | NO | UNI | NULL | |
+----------------+-------------+------+-----+---------+----------------+
role_permission
+---------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| role_id | int(11) | NO | MUL | NULL | |
| permission_id | int(11) | NO | | NULL | |
+---------------+---------+------+-----+---------+----------------+
user_role
+---------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | MUL | NULL | |
| role_id | int(11) | NO | | NULL | |
+---------------+---------+------+-----+---------+----------------+
user_permission
+---------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | MUL | NULL | |
| permission_id | int(11) | NO | | NULL | |
+---------------+---------+------+-----+---------+----------------+
i have migrated to Zend Framework, and having problem deciding wether Zend_Acl allows me to implement the current structure. my question is.
is it possible for me to implement the ACL with current database structure to do the needful in Zend Framework?
is there any better implementation that could allow me to achieve what i want in zend framework?
i will be grateful if someone could provide me a way to get started with what i need to do. any resources, links that could help me?
thank you.
Well I think this structure is really good , to get this working you had to do 2 steps
1-Setup all the databases and requirements
2- create an ACL plugin that determine the user's role and his permissions
some example with doctrine support :
Developing a Doctrine-backed ACL helper TDD-style, part 1
Developing a Doctrine-backed ACL helper TDD-style, part 2
another simple ACL :
Dynamic custom ACL in zend framework?
Why not use the Grants/Roles system already in the database?