How to use Coldfusion Administrator API Security.SetUser() method - coldfusion

I am trying to create an Admin user in ColdFusion 10 using the Administrator API. Below is my code:
<cfscript>
// Create an object of administrator component and call the login method
adminObj = createObject("component","cfide.adminapi.administrator");
adminObj.login("Password1");
myObj = createObject("component","cfide.adminapi.security");
myObj.setUser(username="ramesh"
, password="Password1"
, allowadminaccess="True"
, Roles="Server Settings > Scheduled Tasks"
);
</cfscript>
But it throws an error:
The ROLES argument passed to the setUser function is not of type
array.
How do I pass the roles as an array?

You may be overthinking it ;-) Just put the string inside an array as usual, by enclosing it in square brackets.
myObj.setUser( Roles = [ Roles="Server Settings > Scheduled Tasks" ]
, ...
);
See documentation: Creating and using arrays implicitly

Related

Add a new view in Openart(4) module

In Opencart(4) I am developing a new extension called "testimonials". Created the testimonials and working fine. Now I need to add a new page in the module. So I created a folder named "pages" inside the module's admin template and created a new template file inside it.Also added a function called "showTestPage()" to show the newly created page.
My controller file looks like
...
public function showTestPage(){
$this->load->language('extension/testimonials/module/testimonials');
$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');
$this->response->setOutput($this->load->view('extension/testimonials/module/testimonials/pages/test', $data));
}
When I tried to access the new page using the following url, its showing a permission denied error. Already checked all permissions in "User Group".
http://localhost/op4/admin/index.php?route=extension/testimonials/module/testimonials/showTestPage&user_tok...
Did I missed any steps ? Any idea ?
for the function path in controller do not use slash /
http://localhost/op4/admin/index.php?route=extension/testimonials/module/testimonials/showTestPage&user_tok...
use vertical bar | because framework in OC 4.0.x.x is changed...
http://localhost/op4/admin/index.php?route=extension/testimonials/module/testimonials|showTestPage&user_tok...

Using wildcard route matching in FW/1

I am trying to use the wildcard match on routes in FW/1 v 3.5.
Application.cfc
component extends="framework.one" {
this.name= "Wildcard7";
variables.framework = {
home = 'main.home',
action = 'fuseaction',
baseURL = 'useCgiScriptName',
trace = isDebugMode()
};
variables.framework.routes = [
{ "main/home" = "main/home"},
{ "*" = "main/404"}
];
}
When I run the page, without specifying an action, I get the main/404 page instead of main/home
** FW/1 trace**
How can I get main/404 to run only on invalid pages?
When I run the page, without specifying an action, I get the main/404 page instead of main/home
I assume you are trying to access the page like so - your.domain/index.cfm/main. Note the lack of the home action.
Based on your routes, your first route is saying if the path supplied equals "main/home" then point to the view main/home. If there is an action of home in a main.cfc controller then that will be ran prior to rendering the view.
Leaving off the action, home, would not match any of your current routes; resulting in your wildcard catching it. You would need to handle it by including another route like {"main" = "main"}.
UPDATE:
To access main/home from your.domain/index.cfm, you can try passing a route of {"/" = "main/home"}. I would suggest this being above your wildcard and below any other routes to avoid any freak matches.

Google Directory API - batch add members to a group

I am using the Google Admin SDK to create, update and delete mailing lists (aka groups).
Everything works fine so far and I can create a new group and add members to it. But: Every adding of a member takes about 1s so I was thinking of a batch request to add several users to a group at once.
In the Google Admin interface it is easy to add several users at once but I didn't find any way to implement this via the API.
Is there a way to do so or do I have to loop through every user?
This works but takes a lot of time if I have to do it for every single user:
$service = new Google_Service_Directory($this->getGoogleClient());
$user = new Google_Service_Directory_Member();
$user->setEmail('test#test.com');
$user->setRole('MEMBER');
$user->setType('USER');
$service->members->insert($group_id, $user);
finally I found a solution on my own: The Admin SDK comes with a Batch class :)
To get batch requests working these steps are necessary:
When initiating the Google Client add the following line to the code
$client->setUseBatch(true);
then you can initiate the batch object
$batch = new Google_Http_Batch($client);
a little modification on the code posted above brings me to this code
foreach($arr_users as $user)
{
$userdata = new Google_Service_Directory_Member();
$userdata->setEmail($user);
$userdata->setRole('MEMBER');
$userdata->setType('USER');
$batch->add($service->members->insert($temp_list_name, $userdata));
}
finally you have to execute the request which is done by this line:
$client->execute($batch);
that's all and it works perfectly
While using the method of Christian Lange I was getting this error -
Argument 1 passed to Google\Client::execute() must implement interface Psr\Http\Message\RequestInterface, instance of Google\Http\Batch given,
So I used this instead
$client->setUseBatch(true);
$service = new Google_Service_Directory($client);
$batch = $service->createBatch();
foreach ($emails as $email)
{
$user = new Google_Service_Directory_Member(array('email' => $email,
'kind' => 'member',
'role' => 'MEMBER',
'type' => 'USER'));
$list = $service->members->insert($key, $user);
$batch->add($list);
}
$resultsBatch = $batch->execute();

passing 2-dimensional array in HTTP Request

I'm trying to design a simple API for a webservice I've written. The problem I'm facing is as follows: a form will present to a user any number of themes. This number will change per database.
The user may pick any number of the themes presented and later on can also assign a number value to them to describe level of relevance.
What I need to do is to be able to pass to the webserivce a 2 dimensional array [themeid: theme value] for each selected theme.
I'm not sure how to do that using HTTP get or POST.
Will appreciate any help!
Why do you need a 2d array? Why not pass a list of pairings back where you use an object to encapsulate a theme id and relevance rating pairing? Here is an example in json:
[
{
"rating": 5,
"themeId": 1
},
{
"rating": 3,
"themeId": 2
}
]
whaley has it right pretty much. You want to use json.
Here is an interactive php session to show you how to do this php->json
php > $root = array();
php > $node = new stdClass();
php > $node->theme = "theme";
php > $node->value = "value";
php > $root[12] = $node;
Here's a print_r of this object, to show you what it looks like in php.
php > print_r($root);
Array
(
[12] => stdClass Object
(
[theme] => theme
[value] => value
)
)
Here's an example of the json output.
php > echo json_encode($root);
{"12":{"theme":"theme","value":"value"}}
In essense, you could push this either POST or GET into your application, and json_decode() it to the object in the first section.

Add permission levels to sharepoint list using Web Services

I need to add permission levels to a list like: Full Control, Contribute, Manage Hierarchy, view Only, etc.
I see here: "programatically add user permission to a list in sharepoint", that this can be done using the Object Model. How would I do the same using Web Services?
I tried using the permissions.asmx web services, it works for some of them giving the correct mask like 1011028991 for Approve, 138612833 for read, but it doesn't work for others like Manage Hierarchy, Restricted Read, and any other user created role (permission level). Instead of the correct name I get: Auto-generated Permission Level edda2384-2672-4e24-8f31-071d61a8c303
Any help will be appreciated.
OK, here is a code example, to obtain the mask I based this code on the one from this forum.
string sPermissionName = "Manage Hierarchy"; // if I use Read, Approve, Contribute, Full Control, it works!
string sListName = "testList";
string sGroupName = string.Format("{0}_ManageHierarchy", sListName);
// Create an aux group just to obtain the mask number later
using (SPUserGroup.UserGroup ug = new SPUserGroup.UserGroup())
{
ug.Credentials = new NetworkCredential("user", "pasword");
ug.Url = "http://testSite/_vti_bin/UserGroup.asmx";
ug.AddGroup(sGroupName, "testDomain\\user", "user", "testDomain\\user", "Manage Hierarchy test");
ug.AddGroupToRole(sPermissionName, sGroupName);
}
using (SPPermissions.Permissions per = new SPPermissions.Permissions())
{
per.Credentials = new NetworkCredential("user", "password");
per.Url = "http://testSite/_vti_bin/Permissions.asmx";
XmlNode perms = per.GetPermissionCollection(sListName, "list");
XmlNode n = perms.SelectSingleNode(string.Format("/*[local-name()='Permissions']/*[local-name()='Permission' " +
"and #MemberIsUser='False' and #GroupName='{0}']", sGroupName));
// Here we get the Mask for the role
int iMask = int.Parse(n.Attributes["Mask"].Value);
Console.WriteLine("The mask is:{0}", iMask); // Just to see the mask, I get 2129075183 for Manage Hierarchy
// Here I want to add some user to the list with the specified permission level
// But I get for example: Auto-generated Permission Level edda2384-2672-4e24-8f31-071d61a8c303
// Also, If later I execute the GetPermissionCollection, I see that the mask they got is: 2129075199 and not what I passed which was: 2129075183
per.AddPermission(sListName, "list", "testDomain\\user01", "user", iMask);
per.AddPermission(sListName, "list", "testDomain\\user02", "user", iMask);
}