silverstripe permissions - prevent dataobject from being edited - content-management-system

at the moment I´m checking out the canEdit and canDelete Functions of Dataobject. As far as I can see I have to call that functions always manually in the template or other php code... Is there a way to prevent editing/deleting in general for a certain Dataobject? When I saw the canEdit function the first time I expected it to be checked by silverstripe automatically before writing the DataObject.
So I just want ADMINS to be allowed to write this DataObject:
public function canEdit($member = null){
return(
Permission::checkMember($member = Member::currentUser(), 'ADMIN')
);
}
Regards,
Florian

public function canEdit($member){
if (Permission::check('ADMIN')){
return true;
}else{
// do something here if applicable
}
}
Reference Link 1
Reference Link 2
Reference Link 3

Related

Is there any way to route random string to dashboard in Codeigniter 3?

I have a question about Codeigniter: Is there any way to check function exists In the controller file and then route to the function path or else route it to a specific url?
I don't know what kind of Dashboard mean in your question.
I just answer how to check function exist or not in controller and route to that function.
In some case, you may need to add a route rule in config/routes.php.
$route['/programme/(.*)$'] = 'programme/programmes/$2';
Use method_exists() to check if function exist.
class Programme extends CI_Controller {
public function programmes($sub_part = NULL) {
if (!empty($sub_part) and method_exists($this, 'programmes_'. $sub_part)) {
return $this->{'programmes_'. $sub_part}(array_slice(func_get_args(), 1));
}
die('bar')
}
private function programmes_foo() {
die('foo')
}
}
When a visitor go to https://www.foobar.com/programme/foo (foo is something affect which function to call), your website should able to show foo. If the function is not exist, it show bar.
You can use
$route['product/(:any)'] = 'yourcontroller/your_function';
Source : https://codeigniter.com/userguide3/general/routing.html
i think you can change your route into this
$route[‘produk/(:num)/(:any)’] = ‘HomeControl/detail_produk/$1/$2’;
result from that route it's like this
http://www.localhost.com/produk/1/nama-produk-1
or maybe you can read the documentation CI 3 in this link or you can read my reference this link
i hope i can help you, thanks.

How to use a helper

I am creating a module that allows a user to input details into a form and save their details to allow relevant information to be forwarded to them at specified times.
To retrieve the details from the form and add them to the database I followed a tutorial and produced this code
public function saveAction()
{
$title = $this->getRequest()->getPost('title');
$f_name = $this->getRequest()->getPost('f_name');
$l_name = $this->getRequest()->getPost('l_name');
if ( isset($title) && ($title != '') && isset($f_name) && ($f_name != '') && isset($l_name) && ($l_name != '') ) {
$contact = Mage::getModel('prefcentre/prefcentre');
$contact->setData('title', $title);
$contact->setData('f_name', $f_name);
$contact->setData('l_name', $l_name);
$contact->save();
$this->_redirect('prefcentre/index/emailPreferences');
} else {
$this->_redirect('prefcentre/index/signUp');
}
}
The tutorial says to put it into the controller in a saveAction, and that works fine. However from my very limited understanding it would go in to a helper and i'd call the helper from the controller
I placed the above code in my helper and called it from within the saveAction using the following
Mage::helper('module/helpername');//returned blank screen and did not save
I also tried
Mage::helper('module/helpername_function');//returned error
My config has
<helpers>
<prefcentre>
<class>Ps_Prefcentre_Helper</class>
</prefcentre>
</helpers>
1 . Should this code go in a helper, if not where should it go?
2 . How do I call the helper(or the location the code need to go) to utilise the code?
1 . Should this code go in a helper, if not where should it go?
The actual code you posted imo is predestinated to be used within a controller action because it realizes a very specific process flow: Get user data for a specific case -> save if applicable -> redirect.
Secondly, a helper imo never should control route dispatching. It's a helper, not a controller.
I fail to see any use case where putting the method into a helper would give any advantages.
2 . How do I call the helper(or the location the code need to go) to
utilise the code?
Your definition uses <prefcentre> as alias, so if you did setup the helper the Magento standard way and using the file app/code/local/Ps/Prefcentre/Helper/Data.php, than your helper methods are callable like this:
Mage::helper('prefcentre')->myMethodName();

Differences between _redirect('*/*/') and _redirect('*/*')

I am a newbie to Magento and Zend frameworks. Here are two examples in Mage_Customer_AccountController:
line 127
public function loginPostAction(){
if ($this->_getSession()->isLoggedIn()) {
$this->_redirect('*/*/');
return;
}
and line 230
public function createAction()
{
if ($this->_getSession()->isLoggedIn()) {
$this->_redirect('*/*');
return;
}
What difference will these two _redirect() result in?
These lines are equal, because Magento internally modifies url path to canonical view with "/" at the end.
The reason for writing once '*/*/' and other time '*/*' is just lack of style guidelines describing such cases for Magento developers. You can use any form you like.

Wordpress: Accessing A Plugin's Function From A Theme

I'm trying to add some functionality from a plugin I have made into a Wordpress theme but I am having little joy. The documentation doesn't really help me solve the problem so perhaps someone here can help.
I have a plugin in Wordpress that is activated and working fine. The class for this plugin has a function called generateHtml which I would like to access from a Wordpress Theme. But whatever I try, I cannot seem to access my plugin's code.
Can either give me a summary of what I need to do to get a theme accessing code from a plugin and/or point out there I am going wrong in my code:
Plugin:
<?php
/** Usual comments here **/
if (!class_exists("ImageRotator")) {
class ImageRotator {
private $uploadPath = '';
private $pluginPath = '';
private $options;
function __construct() {
$this->uploadPath = dirname(__file__).'\\uploads\\';
// add_shortcode('imagerotator', array(&$this, 'generateHtml'));
}
// Various functions for plugin
function generateHtml() {
echo '<p>Hello World</p>';
}
}
}
/**
* Create instance of image rotator
*/
$imageRotator = new ImageRotator();
/**
* Create actions & filters for Wordpress
*/
if (isset($imageRotator)) {
// Actions
add_action('admin_menu', array(&$imageRotator, 'createMenu'));
add_action('admin_init', array(&$imageRotator, 'registerSettings'));
add_action('imagerotator_show', array(&$imageRotator, 'generateHtml'));
}
Portion from theme header page:
<?php if (isset($imageRotator)) {
$imageRotator->generateHtml();
} else if (isset($ImageRotator)) {
print_r($ImageRotator);
} else {
echo '<p>Nope!</p>';
}
if (function_exists("imagerotator_show")) {
echo 'Function found';
} else {
echo 'Function NOT found';
}
?>
Currently all I ever see is "Nope" and "Function NOT found". Thanks for any input.
Lee,
For starters, "imagerotator_show" is not a function; it's the name of a type of action. When you use the add_action() function, Wordpress just adds your method to the list of functions/methods to call when a particular action is triggered. Thus your second test will always respond with 'Function NOT found'.
The most likely cause of the first problem is failing to declare the method you want to call as a public method. You're also making the code harder than it needs to be.
The best practice I've seen for declaring methods and registering hooks from a class looks something like this:
if ( ! class_exists( 'Foo' ) ):
class Foo {
function __construct() {
add_action( 'hook_name', array( &$this, 'my_hook_implementation' ) );
}
function my_hook_implementation() {
// does something
}
public function my_special_method() {
// does something else
}
}
if ( class_exists( 'Foo' ) ):
$MyFoo = new Foo();
This allows your class to keep all of its implementation details private. When you need to call my_special_method(), you do it as follows:
$MyFoo->my_special_method();
#andrew since I can't comment I thought I would answer your ancillary question. See:
http://net.tutsplus.com/tutorials/wordpress/create-wordpress-plugins-with-oop-techniques/
Where it is explained that when defining a callback function from an object you have to use the array function. It's basically saying get the function 'my_hook_implementation' from the object $this and use it as the callback parameter to the add action hook. It is because you defined the function within the scope of the object and you have to define the scope in order for PHP to know what function you are talking about. The scope being the object referred to by the variable $this.
You just need to use do_action() function, inside your theme.
If you want the function generateHtml to appears inside your header.php you just need to open the header.php file and paste <?php do_action('imagerotator_show'); ?> where you want and then your function will be called there.

Zend Framework: is there a way to access the element name from within a custom validator?

I'm writing a custom validator that will validate against multiple other form element values. In my form, I call my custom validator like this:
$textFieldOne = new Zend_Form_Element_Text('textFieldOne');
$textFieldOne->setAllowEmpty(false)
->addValidator('OnlyOneHasValue', false, array(array('textFieldTwo', 'textFieldThree')));
My validator will check that only one of those three fields (textFieldOne, textFieldTwo, textFieldThree) has a value. I want to prevent a future developer from accidentally passing the same field twice.
$textFieldOne->addValidator('OnlyOneHasValue', false, array(array('textFieldOne', 'textFieldTwo', 'textFieldThree')));
So far, my validator works perfectly, except when I pass the same field name as the field that has the valiator set on it.
In my validator, you can see that I am checking that the value (of the element with the validator set on it). I'm also checking the values of the other fields that were passed to the validator.
public function isValid($value, $context = null) {
$this->_setValue($value);
$this->_context = $context;
if ($this->valueIsNotEmpty()) {
if ($this->numberOfFieldsWithAValue() == 0) {
return true;
}
$this->_error(self::MULTIPLE_VALUES);
return false;
}
if ($this->numberOfFieldsWithAValue() == 0) {
$this->_error(self::ALL_EMPTY);
return false;
}
if ($this->numberOfFieldsWithAValue() == 1) {
return true;
}
if ($this->numberOfFieldsWithAValue() > 1) {
$this->_error(self::MULTIPLE_VALUES);
return false;
}
}
private function valueIsNotEmpty() {
return Zend_Validate::is($this->_value, 'NotEmpty');
}
private function numberOfFieldsWithAValue() {
$fieldsWithValue = 0;
foreach ($this->_fieldsToMatch as $fieldName) {
if (isset($this->_context[$fieldName]) && Zend_Validate::is($this->_context[$fieldName], 'NotEmpty')) {
$fieldsWithValue++;
}
}
return $fieldsWithValue;
}
My solution is to either...
A. Let the developer figure out there is a certain way to do it.
B. Ignore $value, forcing you to pass all the elements (which isn't much different than the first option).
or C. (if possible) Find the name of the element that called my validator in the first place and ignore it from the list of $fieldsWithValue.
I don't think there is a way to apply a validator on a form without attaching it to an element, but that would be even better, if it were an option.
How can I solve this problem?
Normaly i'd advise against such things, but, in this case I believe a static member in your class would actually provide a good solution to this problem.
With a static member, you can set it to the value in the first time the isValid is called, and check against it in subsequent calls, thus giving you a mechanism for this.
You may want to set this up to use some array in the configuration options, so that you can namespace and allow multiple instances of the validator to exist happily alongside each other for different sets.
The only problem that you really have to decide how to overcome, is where you wish to display the error, as yes the form itself does not take validators. if you want all the duplicates after the first to display an error, it is not so much of a problem.