Sunday, November 14, 2010

Modx 2 How does getService works?

modx->getService($name, $class= '', $path= '', $params= array ())

name = name to be assigned to modx->$name and modx->services[$name]
class = classname (resolving to "classname".class.php)
path = path to look for class
params = array of value pass to class.

Example:

getService("login", "Login", corepath."/components/login", ...);
Resolve to:
core/components/login/Login.class.php
initiate class __construct(modxobject, $params);



Wednesday, November 10, 2010

Modx 2 [revo] with PHPUnit

Test driven development is quite important in developing useful code.
As we test each single functionality, we might find some bug which may exists in the framework we are using.

I'm going to show how phpunit could be use with modx for testing.
First, install phpunit through pear module, which the reference can be found here:

http://www.phpunit.de/manual/current/en/installation.html

Once installed, you can now starting coding by putting up these codes above your test class:
----------------------
define('MODX_API_MODE', true);
//define("MODX_HTTP_HOST", ""); //this is optional, set it to the url of your context
global $modx;
require(dirname(dirname(__FILE__)) . "/index.php"); //path to modx root/index.php
$modx->getRequest();
----------------------

Now, we are ready to create the test unit.
Example:
--------------------------
class TestDB extends PHPUnit_Framework_TestCase {

public function testSuite() {
$this->doTestDatabase();
}

public function doTestDatabase() {
global $modx;
$user = $modx->getObject("modUser", 1);
$this->assertEquals(1, $user->get("id"));
}
}
--------------------------
Running the test:
phpunit TestDB.php

Hope it helps ya :)
Cheers~

XPDO save with addOne

updates: dec 16: My mistake, ive used addOne on a one to many relation, therefore, should use addMany instead of addOne..

-------------

im not sure if this is a bug or the way it should be working.

It seems that if we addOne() to a new object, and save the parent object, the child object and parent object get saved.

But if we edit an existing parent object, and use addOne, the child object did not get saved when i call parentobject->save()...
Please note that the parent object is not modified in my case.


updates: dec 13: relying on addOne to save record is not recommended. Best to set the foreign key value manually.

Hope this help ya :)
Cheers~

Sunday, November 7, 2010

Modx 2 creating child on a protected resource

It seems that, there is some behavior in modx 2 which works kind of odd.
With a resource assigned to resource group, user will need to have 2 different policies under user group to be able to create child under the protected resource.

+ Resource group: "your resource group name", min role: "Member", Access policies: "Administrator", Context: "mgr"
+ Resource group: "your resource group name", min role: "Member", Access policies: "Resource<", Context: "mgr"

Hope it helps for ya ;)
Cheers~

Wednesday, November 3, 2010

Modx 2 Limiting manager screen user to certain context

How do i protect my resources and allow only certain context editable by manager users A?

Filter out context #1 , and only allow context #2 to be edited by user A
First:
Grant permission to the other context (not to be shown to user A) to administrator user group
- Edit Adminisrator user group
- Context Access
- Add context:
+ Context: #1
+ Minimum role: 0
+ Access policy: Administrator

2nd:
Create a role for user A, with ID less than 9999, but above authority level 0, example:
- Role: "Editor"
- authority level: 1000

3rd:
Create the user group for user A:
Example:
- "Editors"

4th: Add user A to user group "Editors"
Security: Access Control: User groups, right click, edit user group "Editor"
Users: Add user to group
Example:
- User: User A
- Role: "Editor"

5th: Goto: Security: Access Control: Access Policies
Right click resource policy, duplicate.
Edit "Duplicate of Resource" policy, permission tab.
Add "resource_tree", and rename policy name to "ResourceTree" and save.
*Note: this will allow showing of resource tree when user is editing context #2


6th: Goto: Security: Access Control: Access Policies, Add policy:
Example:
- Policy: "Editor admin", save. (to be used in manager context for user A)
Go to permission tab.
Add the following permissions:
+ change_profile
+ class_map
+ countries
+ edit_document
+ frames
+ help
+ home
+ load
+ logout
+ resource_tree
+ save_document
+ view
+ view_document
+ new_document
Save.

7th: Goto: Security: Access Control
Right click on "Editors", update user group, Goto context access tab.
Add 3 type of context:
1. Context: mgr, min role: "Editor" (1000), Access policy: "Editor admin"
2. Context: "Context #2", min role: "Editor" (1000), Access policy: "ResourceTree"
3. Context: "Context #2", min role: "member" (9999), Access policy: "Load, list and view"


Now you shall have User A be able to access and edit document in context #2, but context #1 will be hidden from User A.
And anyone logged in as Member of user A will only be able to load,list and view resources created by User A.

And please remember to flush permissions, and relogin the user to test.

How about limiting resources in Context?

This can be done by using resource group.
Example:
Those resources that you do not wish to allow user to edit resource group A
While those resource you wish to allow user to edit as resource group B

1st, setup the resource group A and resource group B.
Then assign resource to resource group accordingly.

2nd, update usergroup "adminisrator",
And goto "Resource group access", and add 2 access:
- Resource group A; min-role: super user; policy: administrator; Context: mgr
- Resource group B; min-role: super user; policy: administrator; Context: mgr
*Note: the above set both group A and B be accessible by administrator users

3rd, update usergroup "Editor",
goto "Context access" tab:
Add 2 access:
- context: mgr; min-role: "editor"; policy: "Editor admin" (as setup above)
- context: "web/mycontext/context #2", min-role: "editor", policy: "load, list, view" (this will allow resource_tree to list resources within the context"

Then, goto "resource group access" tab:
Add access:
- resource group: "resource group B", min-role: "editor"; policy: "administrator", context: mgr (this will allow view/editing/new resource)

Done :)
Hope it helps for you...
Cheers~

Modx 2 Save bar missing

If you ever encounter save bar went missing when you try to edit the resources,
you will realise it only happen to weblink / symlink type of resources.

I realise this is due to the problem of tiny mce plugin, which in my case, might be obsolete due to some upgrading issue.

What you may do is keep uninstall and force-removal on tinymce plugin until you no longer see any version of it.
Then, in package manager, re-add the package again.
Hope it works for ya :)

Cheers~

Tuesday, November 2, 2010

Modx 2 protected content

Modx default permission setting will redirect user to an unauthorised page when the user did not have permission to view the page.

What if you want to show a teaser of the document?
Here is how:
Create the resource, and do not set the page permission.
Instead, set it to a template which output the resource content by a snippet.

Example template:
[[!out.content? &content=`[[*content]]` &guest=`[[*introtext:empty=`[[*content:ellipsis=`600`:striptags]]`]]` &role=`myrole`]]
-------------
This example above will pass in summary of 600 charactors text for guest while output the full content on Logged in with role specified

-------------
Example snippet:
-------------
$aGroups = $modx->getUserDocGroups(true);
if ($aGroups != null) {
foreach($aGroups as $sGroup) {
if (strtolower(trim($sGroup)) == strtolower(trim($role)) ) {
return $content;
}
}
}

return $guest;

Hope it helps ;)
Cheers~