Alfresco Community Rest api, add user to group - alfresco

I am using Alfresco rest api to create users as administrator. When i post data from my form it creates the user but does not assign this user to the group.
public function adduser($user, $pass, $userName, $password, $firstName, $lastName, $email, $group){
$data = array(
"urlPath" => "/people"
);
$params = array(
"userName" => $userName,
"password" => $password,
"firstName" => $firstName,
"lastName" => $lastName,
"email" => $email,
"group" => $group
);
return $this->callAPI($user, $pass, 'POST', $data, $params);
}
Can you help me, what i am missing here?

The problem is that the groups property should be an array, not a simple string. For example, the following JSON can be POSTed successfully to /alfresco/s/api/people:
{
'userName': 'test6',
'password': 'test6',
'firstName': 'test6',
'lastName': 'test6',
'email': 'test6#email.vom',
'groups': ['GROUP_ALFRESCO_ADMINISTRATORS']
}

Related

Integrating wordpress and laravel login

I am developing three area for my website.. using WordPress (Front Page), XenForo (Forum), and Laravel (Member Area) ... i already succeed integrating my laravel and XenForo, so when XenForo user login to Member Area my function will check if the user already exist (If XenForo user exist then laravel will create the user in database using XenForo user data, if not exist then login failed)
Now i want to do the same for WordPress .. when user login to member area the function will check if WordPress user is exist or not .. if exist then new user will be created using wordpress user data (name, email, password, and token if possible) if not exist then login failed, but how do i do this? unlike XenForo .. i can't find WordPress API documentation for getting user data
Edit 1 :
This is my login function
public function login_post(Request $request){
// XENFORO API AUTH START
$http = Http::withHeaders([
'XF-Api-Key' => 'SECRET'
])->asForm()->post('http://forum.mywebsite.com/api/auth', [
'login' => $request->email,
'password' => $request->password
]);
if(isset($http['errors'])){
return redirect('login')->with('error', 'Username and Password Error');
}
$name = $http['user']['username'];
$email = $http['user']['email'];
$password = $request->password;
$is_admin = $http['user']['is_admin'];
$user_id = $http['user']['user_id'];
// XENFORO API AUTH END
// WordPress API AUTH START
$client = new \GuzzleHttp\Client([
// Base URI is used with relative requests
'base_uri' => 'http://mywebsite.com/',
'headers' => ['Content-Type' => 'application/json', "Accept" => "application/json", 'Authorization' => 'Basic ' . base64_encode( 'ADMIN' . ':' . 'PASSWORD' ),],
]);
$response = $client->get('users/', [
'query' => [
'email' => $request->email,
]
]);
return json_decode($response->getBody());
// WordPress API AUTH END
if (Auth::attempt(['email' => $email, 'password' => $password])) {
# code...
if(Auth::user()->role == 0){
return redirect('dashboard');
}
return redirect('login');
} else {
$newUser = new User;
$newUser->name = $name ;
$newUser->email = $email;
$newUser->password = bcrypt($password);
$newUser->role = $is_admin;
$newUser->save();
auth()->login($newUser, true);
return redirect('dashboard');
}
}
Edit 2 :
$client = new \GuzzleHttp\Client([
// Base URI is used with relative requests
'base_uri' => 'http://mywebsite.com/wp-json/wp/v2/',
'headers' => ['Content-Type' => 'application/json', "Accept" => "application/json", 'Authorization' => 'Basic ' . base64_encode( 'ADMIN' . ':' . 'PASSWORD' ),],
]);
$response = $client->get('users/', [
'query' => [
'email' => $request->email,
]
]);
$response = json_decode($response->getBody());
$name = $response['username'];
$email = $response['email'];
$password = $request->password;
$is_admin = $response['role'];
$user_id = $response['id'];
The Response :
array:3 [▼
0 => {#287 ▼
+"id": 1
+"name": "Admin"
+"url": "http://www.mywebsite.com"
+"description": ""
+"link": "http://mywebsite.com/author/admin/"
+"slug": "admin"
+"avatar_urls": {#285 ▶}
+"meta": []
+"_links": {#281 ▶}
}
1 => {#274 ▼
+"id": 2
+"name": "Prima"
+"url": ""
+"description": ""
+"link": "http://mywebsite.com/author/prima/"
+"slug": "prima"
+"avatar_urls": {#272 ▶}
+"meta": []
+"_links": {#279 ▶}
}
2 => {#288 ▼
+"id": 3
+"name": "TEST ER"
+"url": ""
+"description": ""
+"link": "http://mywebsite.com/author/testing/"
+"slug": "testing"
+"avatar_urls": {#289 ▶}
+"meta": []
+"_links": {#291 ▶}
}
]
WordPress doesn't provide API to check user exists or not, however you can check this by using List Users API.
Here is what I have done in Laravel:
$client = new \GuzzleHttp\Client([
// Base URI is used with relative requests
'base_uri' => 'http://www.example.com',
'headers' => ['Content-Type' => 'application/json', "Accept" => "application/json"],
]);
$response = $client->get('users/', [
'query' => [
'search' => 'example#email.com',
]
]);
return json_decode($response->getBody());
With Authorization:
Install this https://github.com/WP-API/Basic-Auth plugin and update headers, $username & $password refers to WP admin credentials.
$client = new \GuzzleHttp\Client([
// Base URI is used with relative requests
'base_uri' => 'http://www.example.com',
'headers' => ['Content-Type' => 'application/json', "Accept" => "application/json", 'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ),],
]);

Symfony3 :: Handle ManyToMany relations

I'm trying to save my ManyToMany relations between users and categories. Actually I'm trying to save my category with given users, but this doesn't work.
Form
$builder->add('name')
->add('users', EntityType::class, array(
'label' => 'Benutzer',
'class' => 'AppBundle\Entity\User',
'multiple' => true,
'expanded' => true,
'required' => false,
'choice_label' => function (User $user) {
return $user->getUsername();
}
))
->add('submit', SubmitType::class, array(
'label' => 'Speichern'
));
Form Handler
public function onSuccess(Request $request)
{
// Get category from form
$category = $this->getForm()->getData();
// Redirect to parent category when setted
if ($this->parent) {
$category->setParent($this->parent);
$response = new RedirectResponse($this->router->generate('categories.view', [
'category' => $this->parent->getId()
]));
} else {
// Build new redirect response
$response = new RedirectResponse($this->router->generate('categories.view', [
'category' => $category->getId()
]));
}
try {
// Save category in database
$this->em->merge($category);
$this->em->flush();
} catch (ORMException $ex) {
throw $ex;
}
return $response;
}
Maybe you have to unserialize the Entity $category first?
$detachedCategory = unserialize($category);
$this->em->merge($detachedCategory);
$this->em->flush();
I found this link regarding that:
How to manage deserialized entities with entity manager?
Not sure if that's the answer, but you might want to do more research.

cakephp no login after facebook auth

when i create a user with facebook-sdk, i don't get him logged in automaticaly after facebook-login.
Usercreation working well, but no login.
Cakephp and facebook-sdk are latest versions.
here is my accountController:
<?php
class AccountController extends AppController {
public $uses = array('User');
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('login');
#$this->Auth->authenticate = array('Basic' => array('fields' =>array('username' => 'fb_id', 'password' => 'random')));
}
public function login() {
$this->facebook = new Facebook(array(
'appId' => '14549077xxxx',
'secret' => '95a38f3xxxx',
'cookie' => 'true'
));
$user = $this->facebook->getUser();
if($user){
try{
$params = array('next' => 'http://'.$_SERVER['HTTP_HOST'].'/account/logout');
$this->Session->write('logoutLink', $this->facebook->getLogoutUrl($params));
#if (!$this->Auth->user()) {
$fb_user = $this->facebook->api('/me');
$authUser = $this->User->findByFbId($user);
if (empty($authUser)) {
$pw = $this->__randomString();
$authUser = array(
'fb_id' => $user,
'random' => $pw,
'password' => $this->Auth->password($pw),
'email' => $fb_user['email']
);
$this->User->create();
$this->User->save($authUser);
$authUser = $this->User->findByFbId($user);
}
$this->Auth->authenticate = array(
'Form' => array(
'fields' => array('username' => 'fb_id', 'password' => 'random')
)
);
$this->Auth->login($authUser['User']);
$this->redirect($this->Auth->redirect());
#}
}catch(FacebookApiException $e){
$user = NULL;
}
}
if(empty($user)){
$loginurl = $this->facebook->getLoginUrl(array(
'scope'=> 'email,publish_stream,read_friendlists',
'redirect_uri' => 'http://'.$_SERVER['HTTP_HOST'].'/account/login',
));
$this->redirect($loginurl);
}
}
public function dashboard() {
echo 'logged in';
}
}
Users will always get redirect to login page.
$authUser is always filled correctly.
Pls Help :)
Greetings
m.
my AppController
class AppController extends Controller {
public $components = array('Auth','Session');
public function beforeFilter() {
$this->Auth->loginRedirect = array('controller' => 'account', 'action' => 'dashboard');
$this->Auth->logoutRedirect = '/';
$this->Auth->loginAction = array('controller' => 'pages', 'action' => 'login');
}
Sessions:
Configure::write('Session', array(
'defaults' => 'php'
));
problem solved in chat:
Facebook is creating a new session,
$this->facebook = new Facebook(array(
'appId' => '14549077xxxx',
'secret' => '95a38f3xxxx',
'cookie' => 'true'
));
this code has to written down in appcontroller
public function beforeFilter() {
$this->Auth->loginRedirect = array('controller' => 'account', 'action' => 'dashboard');
$this->Auth->logoutRedirect = '/';
$this->Auth->loginAction = array('controller' => 'pages', 'action' => 'login');
$this->facebook = new Facebook(array(
'appId' => '14549077xxx',
'secret' => '95a38f3xxx',
'cookie' => 'true'
));
}
Thank you so much #noslone for trying together :D
Providing data $this->Auth->login($authUser['User']); to log a user in is wrong.
As per CakePHP documentation:
In 2.0 $this->Auth->login($this->request->data) will log the user in with whatever data is posted, whereas in 1.3 $this->Auth->login($this->data) would try to identify the user first and only log in when successful.
You should do something like this:
$this->request->data['User'] = $authUser['User'];
$this->request->data['User']['password'] = $authUser['User']['random'];
$this->Auth->login();
I have also added $this->request->data['User']['password'] = $authUser['User']['random']; because i think it fails to authenticate when providing the encrypted password to login.
If you have any other trouble, I am developing a Plugin that uses Facebook oAuth as an authentication object for Auth Component. If you want an already-built solution that uses server-side Facebook login, please check my website: http://marianofino.github.com/Facebook-Plugin-for-CakePHP/

Drupal: assign roles in user_save

I can't find a solution or right example for something that should be quite simple: assign a role to an user when creating it, this is what I'm trying:
$edit = array(
'name' => $_POST['name'],
'pass' => $password,
'mail' => $_POST['email'],
'status' => 0,
'language' => 'es',
'init' => $_POST['email'],
array(2 =>'authenticated', 4=>'my custom role') //as id and named in role db table
);
user_save(NULL, $edit);
The user is not being created, how can I do this?
Thank you
You haven't named the roles member as such. Try his modified version:
$edit = array(
'name' => $_POST['name'],
'pass' => $password,
'mail' => $_POST['email'],
'status' => 0,
'language' => 'es',
'init' => $_POST['email'],
'roles' => array(
2 => 'authenticated',
4 => 'my custom role',
),
);
user_save(NULL, $edit);
And you can use objects to do that.
// Check if user's email is unique
if (!user_load_by_mail($_POST['email'])) {
$account = new stdClass;
$account->name = $_POST['name'];
$account->pass = user_hash_password($password);
$account->mail = $_POST['email'];
$account->status = FALSE;
$account->language = 'es';
$account->init = $_POST['email'];
$account->roles = array(
DRUPAL_AUTHENTICATED_RID => TRUE,
'Your custom role' => TRUE,
);
user_save($account);
}
Here is a hook I've written to add a role to a user when a new user is inserted:
<?php
function MYMODULE_user_insert(&$edit, $account, $category){
if (array_key_exists('profile_1', $account)) {
$is_university = FALSE;
if ($account->profile_sport_club['field_club']['und'][0]['value'] == 1 ) {
$is_university = TRUE;
}
if ($is_university) {
$uid = $account->uid;
$role_name = 'uni_club';
if ($role = user_role_load_by_name($role_name)) {
user_multiple_role_edit(array($uid), 'add_role', $role->rid);
}
}
}
}
?>
Thanks to this tip, it's now much simpler.
function first_user_insert(&$edit, $account, $category, $node){
$uid = $account->uid;
$role_name = 'role name';
if ($role = user_role_load_by_name($role_name)) {
user_multiple_role_edit(array($uid), 'add_role', $role->rid);
}
}

wordpress create user from facebook login plugin

How do I call this function from my wordpress theme. It is supposed to be called when the one who comments logs in via facebook login function.
function myfb_do_login() {
global $wpdb;
// cookie
$cookie = get_facebook_cookie();
// get user data
$fbuser = get_facebook_user($cookie);
$username = sanitize_user($fbuser->first_name);
// put everything in nice array
$userdata = array(
'user_pass' => wp_generate_password(),
'user_login' => $username,
'user_nicename' => $username,
'user_email' => $fbuser->email,
'display_name' => $fbuser->name,
'nickname' => $username,
'first_name' => $fbuser->first_name,
'last_name' => $fbuser->last_name,
'role' => 'subscriber'
);
// create new user
$new_user = wp_insert_user($userdata);
// set the auth cookie to current user id
wp_set_auth_cookie($new_user, true);
// log the user in
wp_set_current_user($new_user);
// do redirect here
wp_safe_redirect(get_permalink(). '#response');
}
if you have user info at hand, create an array of user data an pass it to wp_insert_user, what userdata should contains refers to wp_insert_usercodex.
code example:
//insert new user to db
$wpuid=wp_insert_user($userdata);
//set the auth cookie to current user id
wp_set_auth_cookie($wpuid,true);
//log the user in
wp_set_current_user($wpuid);
//do redirect here....
wp_safe_redirect($location);

Resources