Get current user access level in Joomla 2.5 - php

In Joomla 2.5.14, I have a script that gets the current user group id, like:
$groups = $user->get('groups');
foreach($groups as $group) {
echo "<p>Your group ID is:" . $group . "</p>";
};
Now, I need to count the number of articles this user is able to read, given his access level.
For that, I need to select the access level from the viewlevels table, that looks like this:
id title rules JSON encoded access control.
1 Public [1]
2 AccessA [13,8,16,17]
3 AccesssF [8]
4 AccessD [14,8]
6 AccessB [8,16,17]
7 AccessC [8,17]
So, for example, Group 17 may read the articles in AccessA, AccessB and Access C.
I tried the following query, but it isn't selecting any rows:
$query="SELECT * FROM xmb9d_viewlevels WHERE rules LIKE '%.$group.%'";
How can I select all the acess levels for the current user group and then count the number of articles he's able to read?
Thanks for helping!

First
https://github.com/joomla/joomla-cms/blob/master/libraries/joomla/access/access.php#L285
JAccess::getGroupsByUser($userId, $recursive = true)
Will get you the groups a user is matched to including via inheritance (unless you make $recursive false)
https://github.com/joomla/joomla-cms/blob/master/libraries/joomla/access/access.php#L402
JAccess::getAuthorisedViewLevels($userId) will give you all the view levels a user is allowed to see as an array.
If you do
$allowedViewLevels = JAccess::getAuthorisedViewLevels($userId);
if nothing else you could do
$implodedViewLevels = implode(',', $allowedViewLevels);
....
$query->select($db->quoteName('id'))
->from('#__content')
->where($db->quoteName('access') . 'IN (' . $implodedViewLevels . ')';
....
(not tested but you get the general idea).
In joomla always try to let the api do the work for you rather than fight with it.

I think you just need to remove the periods from within the LIKE clause. Such as:
$query="SELECT * FROM xmb9d_viewlevels WHERE rules LIKE '%$group%'";
Also, to make it not specific to the database you should substitute #__ (Note: it's two underscores) for the table prefix:
$query="SELECT * FROM #__viewlevels WHERE rules LIKE '%$group%'";

Related

Looping through the values of an array intersect

I have different communities in my database, such as: community_newsfeed, community_marketplace, community_games_requested, community_games_offered, events and custom_posts.
Each of those communities stores a post_id, as well as the user_id.
Then I have a community_users table that stores user_id and community_id.
I want to create a mechanism which I, as the authenticated user, can view all the posts of a user, as long as that user and I belong to the same community.
And on that regard, I must view only the posts for the communities which we both belong to.
So I do the following:
Get all the community id's of the communities which I(auth user) belong to:
$community_user_member = DB::table('community_users')->whereUserId($user->id)->pluck('community_id')->toArray();
Then I do the same for the user which I want to see the posts:
$requested_user = DB::table('community_users')->whereUserId($user_id)->pluck('community_id')->toArray();
Fine. Now I use array interect to give me all the community_id that we share only:
$members = array_intersect($community_user_member, $requested_user);
Now, this is where I get stuck.
How can I get the communities which the id match the values I get in the $members value(not the key) and the user id matches $user_id( this is passed in the url )?
if(!empty($members)){
$newsfeed = CommunityNewsfeed::whereIn('newsfeed_community_id', $members)->whereUserId($user_id)->get();
}
I am sure that this user has 4 posts from community_newsfeed table, but I only see two posts, and also I dont see the correct posts.
Live example:
User A belongs to community_newsfeed.
User B belongs to community_newsfeed and community_marketplace.
User A must see the community_newsfeed data where user_id is the id of User B.
Updated
Another option I tried was to use the in array function:
$common = [];
foreach ($community_user_member as $key => $member) {
if (in_array($member, $requested_user)) {
$common[] = $member;
}
}
print_r($common);
The end result should be something like this:
$common = [ '4','5','10','20' ];
Because then I would be able to loop through $common array and do whatever I want.
But I get the following error when I refresh the page:
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

Laravel weird behavior orderByRaw(field())

I'm creating a multi roll registration system. And I'm trying to list all existing rolls. These have to be sorted ascendingly on the amount of permissions they have assigned to them. (in this example: User first, Moderator second and Admin third)
I cannot define the rolls in the code because new rolls can be created and existing rolls can be deleted. (exept for the user and admin roll)
I retrieve the permission count of all the available rolls and place them in a array with the id of the roll.
the array per_count returns the following ids:
2, 4, 3
Id's:
2 = User
3 = Admin
4 = Moderator
However, When I execute the following query:
Roll::orderByRaw("FIELD(id, ?)", array($per_count))->get()
It reverses the order and I get
3 Admin
4 Moderator
2 User
Controller:
public function index()
{
// Check if user is authorized to use this action, else redirect to login page
$this->authorize_action(__FUNCTION__, $this->classname);
foreach (Roll::all() as $roll) {
$rollpermission['id'] = $roll->id;
$rollpermission['count'] = $roll->permissions->count();
$permission[] = $rollpermission;
}
$this->array_sort_by_column($permission, 'count');
$per_count = array();
foreach ($permission as $per) {
$per_count[] = $per['id'];
}
$foo = implode(', ', array_fill(0, count($per_count), '?'));
return view('roll')
->with('roll', Roll::orderByRaw("FIELD(id, " . $foo . ") desc", array_reverse($per_count))->get());
}
I managed to get it working correctly with the array_reverse function.
However it is sloppy and should not be necessary. And I cant find the reason why the query reverses the results.
Can somebody explain to me why the results are reversed? Thanks
orderByRaw() expects the second parameter to be an array of bindings, not an array to implode for one binding. But on the other hand, if you implode it, it will be passed in as one string '2, 3, 4', when you need a comma delimited list of numbers. Do this instead:
Roll::orderByRaw("FIELD(id, " . implode(", ", $per_count) . ")")->get();
Which will yield:
select * from `[table_name]` order by FIELD(id, 2, 3, 4)
Also, you may want to check the difference between roll and role before you code too much ;)

CakePHP & MVC – Potentially superfluous SQL queries when looking up 'names' associated with ids

I've probably murdered the whole concept of MVC somewhere along the line, but my current situation is thus:
I have participants in events and a HABTM relationship between them (with an associated field money_raised). I have a controller that successfully creates new HABTM relationships between pre-existing events and participants which works exactly as I want it to.
When a new relationship is created I wish to set the flash to include the name of the participant that has just been added. The actually addition is done using ids, so I've used the following code:
public function addParticipantToEvent($id = null) {
$this->set('eventId', $id);
if ($this->request->is('post')) {
if ($this->EventsParticipant->save($this->request->data)) {
$participant_id = $this->request->data['EventsParticipant']['participant_id'];
$money_raised = $this->request->data['EventsParticipant']['money_raised'];
$participant_array = $this->EventsParticipant->Participant->findById($participant_id);
$participant_name = $participant_array['Participant']['name'];
$this->Session->setFlash('New participant successfully added: ' . $participant_name . ' (' . $participant_id . ') ' . '— £' . $money_raised);
} else {
$this->Session->setFlash('Unable to create your event-participant link.');
}
}
}
This works, but generates the following SQL queries:
INSERT INTO `cakephptest`.`cakephptest_events_participants` (`event_id`, `participant_id`, `money_raised`) VALUES (78, 'crsid01', 1024) 1 1 0
SELECT `Participant`.`id`, `Participant`.`name`, `Participant`.`college` FROM `cakephptest`.`cakephptest_participants` AS `Participant` WHERE `Participant`.`id` = 'crsid01' LIMIT 1 1 1 0
SELECT `Event`.`id`, `Event`.`title`, `Event`.`date`, `EventsParticipant`.`id`, `EventsParticipant`.`event_id`, `EventsParticipant`.`participant_id`, `EventsParticipant`.`money_raised` FROM `cakephptest`.`cakephptest_events` AS `Event` JOIN `cakephptest`.`cakephptest_events_participants` AS `EventsParticipant` ON (`EventsParticipant`.`participant_id` = 'crsid01' AND `EventsParticipant`.`event_id` = `Event`.`id`)
This final one seems superfluous (and rather costly) as the second should give me all that I need, but removing $this->EventsParticipant->Participant->findById($participant_id) takes out both the second and third queries (which sort of makes sense to me, but not fully).
What can I do to remedy this redundancy (if indeed I'm not wrong that it is a redundancy)? Please tell me if I've made a complete hash of how these sorts of things should work – I'm very new to this.
This is probably due to the default recursive setting pulling the relationship. You can remedy this by setting public $recursive = -1; on your model (beware this will affect all find calls). Or, disable it temporarily for this find:
$this->EventsParticipant->Participant->recursive = -1;
$this->EventsParticipant->Participant->findById($participant_id);
I always suggest setting public $recursive = -1; on your AppModel and using Containable to bring in the related data where you need it.

Complex Zend Query from same user table

I have a rather unique set of conditions and orders in which I need to retrieve data from a "sellers" table for an application I'm building in Zend framework.
The client is basically requesting an application where the directory page lists sellers in a very particular order, which is:
Sellers who have been approved in the last 7 days (then order by #4 below)
Then, selllers who have paid for upgraded features on the site, and are more the 7 days old (then order by #4 below)
Then, Sellers who are more than 7 days old and are more than 7 days old (then order by #4 below)
For all of the above, secondary order by would be their launch date, then alpha by business name
I'm trying to figure out the most effective way to write an action helper that will return the data in the correct sequence above, knowing that some of my views only need 1,2 (and 4), whereas other views within the application will need all 4.
Right now, I've been writing two or three separate queries, and passing them to 2 or 3 partialloop's inside the view, but I strive for properly written code, and would like to either combine my 3 queries into one object I can pass to one partial loop, or.... write one query. How can this be done?
Here's my helper at the moment:
class Plugin_Controller_Action_Helper_ListSellers extends Zend_Controller_Action_Helper_Abstract
{
//put your code here
public function direct($regulars = false, $filter = false)
{
$dateMod = $this->dateMod = new DateTime();
$dateMod->modify('-7 days');
$formattedDate = $dateMod->format('Y-m-d H:i:s');
// get sellers initialized in last 7 days
$sellerTable = new Application_Model_DbTable_Seller();
// get sellers initialized in last 7 days
$select = $sellerTable->select()->setIntegrityCheck(false);
$select->from(array('b' => 'seller'),array('sellerID', 'businessName','sellerPicture'));
// select firstName, lastName, picture from user table, and businessName and sellerID from seller table. All records from seller table
$select->join(array('u' => 'user'), 's.userID = u.userID', array('firstName', 'lastName'));
$select->order('s.launchDate DESC','s.businessName ASC');
$select->where('s.active = 1 AND s.contentApproval = 1 AND s.paymentApproval = 1');
$select->where('s.launchDate > ?', $formattedDate);
if($filter){ $select->where('s.categoryID = ?', $filter);}
$newSellers = $sellerTable->fetchAll($select);
$query = $sellerTable->select()->setIntegrityCheck(false);
$query->from(array('b' => 'seller'),array('sellerID', 'businessName','sellerPicture'));
// select firstName, lastName, picture from user table, and businessName and sellerID from seller table. All records from seller table
$query->join(array('u' => 'user'), 's.userID = u.userID', array('firstName', 'lastName'));
$query->order('s.launchDate DESC','s.businessName ASC');
$query->where('s.active = 1 AND s.contentApproval = 1 AND s.paymentApproval = 1 AND s.featured = 1');
$query->where('s.launchDate < ?', $formattedDate);
if($filter){ $select->where('s.categoryID = ?', $filter);}
$featuredSellers = $sellerTable->fetchAll($query);
if($regulars){
$where = $sellerTable->select()->setIntegrityCheck(false);
$where->from(array('b' => 'seller'),array('sellerID', 'businessName','sellerPicture'));
// select firstName, lastName, picture from user table, and businessName and sellerID from seller table. All records from seller table
$where->join(array('u' => 'user'), 's.userID = u.userID', array('firstName', 'lastName'));
$where->order('s.launchDate DESC','s.businessName ASC');
$where->where('s.active = 1 AND s.contentApproval = 1 AND s.paymentApproval = 1 AND s.featured IS NULL');
$where->where('s.launchDate < ?', $formattedDate);
$regularSellers = $sellerTable->fetchAll($where);
}
}
}
I don't see any limits being applied to your queries. So does that mean you really want to select all matching records? For scalability reasons I'd guess that the answer should be no, there will be limits applied. In this case, you may just have to do 3 different queries.
But if there are no limits to be applied, then you could do a single simple query that selects all sellers, unfiltered and unsorted, and do your sorting and filtering in view helpers or just in your views.
Regardless, I recommend not putting database queries inside your controller layer, assuming you want to use the Model-View-Controller pattern which Zend is built for. Controllers should be thin. Your models should handle all database queries and just spit out the results into your controllers. I use the Data Mapper pattern extensively. Something like:
$mapper = new Application_Model_SellerMapper();
$newSellers = $mapper->fetchNewSellers();
$featuredSellers = $mapper->fetchFeaturedSellers();
$regularSellers = $mapper->fetchRegularSellers();
Each of your fetchX() methods would return an array of Application_Model_Seller instances, rather than Zend_Db_Table_Row instances.
This way you maintain Separation of Concerns and Single Responsibility Principle better, for more maintainable code. Even if you're the only developer on the project over the long-term, 6 months from now you won't remember what you wrote and why. And if someone else comes on the project, clarity becomes really important.

MongoDB: updating collection of objects to change owning side of reference

I have two collections, Group and User. Originally, my Group document contained an array of User references. But I've changed my mapping so that Users now hold a reference to a Group. I am wondering how I can write a query to update all the existing User documents to reference the group that is currently referencing them, then remove any references on the groups.
I didn't notice the language you are using at first - I had to do something very similar in Python so here is my example code:
# I changed my collection names to users/groups hope I mapped them right :)
users = db.users
groups = db.groups
for u in users.find():
grp = groups.find( { "users" : u['userId'] } )
u['groups'] = [ g['groupId'] for g in grp ]
users.save(u)
Basically, for every user, I look in groups to find a list of groups which contain userId in the array users, and I make a list called groups in u and when I save it adds an array of groups to users document.
Should be not hard to do it in PHP
$users = $db->users;
$groups = $db->groups;
// find all users
$ucursor = $users->find();
// iterate through users, find groups which have the user,
// update user with array of groups
foreach ($ucursor as $u) {
$gcursor = $groups->find( ... );
foreach ($gcursor as $g) {
...
}
}
But this seems like a one time operation, you could do it in mongo shell using something similar to:
var users = db.users.find();
while (users.hasNext()) {
u = users.next();
var gs = db.groups.find({"users":u.userId},{"groupId:1}).toArray();
db.users.update({"groupId":groupId},{$set:{"groups":gs}});
}

Categories