How to assign an Owner using the Hubspot API? - hubspot

I'm using the Hubspot API to create new contacts and would like to automatically set the owner when the contact is created. I know this is possible without using the API via Workflows, however I'd like to use the API for that.
Here's my code right now (which works, just missing the contact owner):
$data = [
'properties' => [
['property' => 'firstname', 'value' => $contact->first_name],
['property' => 'lastname', 'value' => $contact->last_name],
]
];
$body = json_encode($data);
$response = $client->request('POST', '/contacts/v1/contact/email/'.$user->email.'/profile',
['query' => ['hapikey' => $hubspot_key, 'body' => $body]);

I eventually found a way to achieve this:
Go to your Hubspot instance, then Settings and Properties
Search for "Contact Owner" and click on Edit
In "Field type", identify the Owner's ID value
Then in your API call, simply pass this property:
$data['properties'][] = ['property' => 'hubspot_owner_id', 'value' => 123456];
You can find out more about the hubspot_owner_id property (which is internal to Hubspot, this is not a custom property) in Hubspot's documentation.
It will automatically assign the newly created (or updated) Hubspot contact to the related Owner (Hubspot User).

Related

Creating auth tokens for a custom model that is not the default User model

How do I create access/api/auth tokens for a different model? From most questions, docs, or tutorials, most are using the default User model.
I read the default doc but it doesn't really say where to substitute the default Model class or how to verify against non-Model classes.
Any suggestions?
To use a different model than User for Laravel Sanctum API authentication.
This is for Laravel 8.
Create new model, php artisan make:model ModelName -m
the flag m is used to instantiate a migration file for this model.
Go to the Model class file and extend it with Illuminate\Foundation\Auth\User, ensure it uses HasApiTokens, and list out your fillable fields for record creation.
...
use Illuminate\Foundation\Auth\User as Authenticatable;
class ModelName extends Authenticatable{
use ..., HasApiTokens;
protected $fillable = [...]
}
Go to config/auth.php and add new provider and new guard.
'guards' => [
...
,
'api' => [
'driver' => 'sanctum',
'provider' => 'model-name',
'hash' => false,
]
],
'providers' => [
...
,
'model-name' => [
'driver' => 'eloquent',
'model' => App\Models\ModelName::class,
]
]
Go to your api routes and wrap your routes as below.
Route::middleware(['auth:sanctum'])->group(function(){
Route::get('/whatever-route-name',function(){
return 'Authenticated';
});
});
Download Postman or your favored API testing tool, send a GET request to [http://localhost:8000/api/whatever-route-name](http://localhost:8000/api/whatever-route-name) , in Headers, ensure Accept has a value of applcation/json, send the request, and it should return an {”message”: “Unauthenticated.”}
Go to your public routes, create a dummy route to create a record for ModelName
After creation ensure that you call $model_name→createToken($model_name→whatever_field)→plaintTextToken; to get the plain text api key.
Go to back to your API test tool, under Authorization, choose Bearer Token and supply the api key returned from above.
The route wrapped in auth:sanctum is now accessible.

How can I use another table for authentication in Laravel 7

Im very new with Laravel and had already seen many tutorials, so now I know little more. But I am struggling with a problem that I can't solve. I want to use another table(t_clients) that already exists in my DB instead of users table made by migration for authentication. I already read the documentation on website , so I changed in auth.php this:
'providers' => [
// 'users' => [
// 'driver' => 'eloquent',
// 'model' => App\User::class,
// ],
'users' => [
'driver' => 'database',
'table' => 't_clients',
],
],
Now when I try to login I get the next error : ErrorException Undefined index: id. So I tried the next one and this problem is temporary fixed, I changed the primary_key name in the table "t_clients" to "id" and login works. The original column name is "pk_client". But I want actually that the primary_key remains unchanged. I want actually that by authentication, Laravel search for "pk_client" and not "id". Does anyone know how I can fix this? I appreciate your help.
Spec project:
Laravel Framework 7.1.0
mysql Ver 8.0.18
This problem was fixed by making a Client EloquentModel and add de next lines:
protected $table = 't_clients';
protected $primaryKey = 'pk_client';
So the result should be:
class Client extends Authenticatable implements CanResetPassword
{
protected $table = 't_clients';
protected $primaryKey = 'pk_client';
use Notifiable;
}

Get user roles through Moodle Webservice

I’ve been trying to find a way to get a user’s roles back through the Moodle webservice API.
I know there are no endpoints to do this but I cannot retrieve them directly from the database because I do not have access to the client’s database.
Is there another way to do that?
You may code a solution along this lines:
Create a barebones plugin with the usual boilerplate code (version.php and so on), for example a local plugin. You can use this other plugin to generate the boilerplate code: https://moodle.org/plugins/tool_pluginskel
In 'db/services.php' register a new external function and expose it in a new or existing service. Docs here: https://docs.moodle.org/dev/Adding_a_web_service_to_a_plugin and here https://docs.moodle.org/dev/Web_services_API . For example:
$functions = [
'local_myplugin_get_user_roles' => [
'classname' => external::class,
'methodname' => 'get_user_roles',
'description' => 'gets user roles',
'type' => 'read',
],
];
$services = [
'My services' => [
'functions' => [
'local_myplugin_get_user_roles',
],
'enabled' => 1,
'restrictedusers' => 0,
'shortname' => 'local_myplugin',
'downloadfiles' => 0,
'uploadfiles' => 0,
],
];
Write an external class (for example in a external.php file) for your plugin (referenced in the function definition you coded before). In this class write the code for the defined external function (that will fetch the roles for a user given a user id, for example), including the input and output handlers. Example here: https://docs.moodle.org/dev/External_functions_API#externallib.php
Within your external function, to get a list of user roles given a context, userid, etc. you may use the global helper get_user_roles. Do not forget to write in this external function the code needed to validate input parameters and so on.
To properly expose your new service and external function to an external system you need to follow this settings guidelines as a Moodle admin: YOUR_MOODLE_INSTANCE_URL/admin/settings.php?section=webservicesoverview . At the end you will generate a user (web service consumer) and a token, that you can set in your external system to consume the Moodle service.
Happy coding.

How to send user login credentials after user registers

I am using laravel's default login and registration. I have successfully set up authentication, however I would like to send user's username and password to the emails they used during registration. How can I achieve this?
You can initiate a mail to user after you validated input received and before your create functions of your register controller present in
$email = new UserRegisterData(new User(['password' => $user->password, 'name' => $user->name]));
To do this you need to rewrite
protected function validator(array $data)
function in your register controller and modify that with this mail. as
protected function validator(array $data)
{
$email = new UserRegisterData(new User(['password' => $user->password, 'name' => $user->name]));
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:customers',
'password' => 'required|min:6|confirmed',
]);
}
once you hashed your password in create function, i think it can't be read. Also check if any security issue it may generate. Also you need to create mail as 'UserRegisterData' and add necessary code in it.

Drupal 7 - how to allow an application to access a certain drupal url (from menu hook), either with super basic url-based auth or anonymously

I'm pretty new to drupal, so bear with me if I'm not using correct terms.
I'm trying to give a stupidly basic application access to a drupal url - this application cannot do any complicated authentication.
Otherwise, this drupal system needs authentication and all other menu hooks use 'access arguments' => array('access content')
Even before looking a solution for easy authentication method with drupal like http://user:password#server.com/awesome/member/12345, I've tried just giving anonymous access with the following code block in a drupal .module file
function awesome_module_menu() {
$items['awesome/member/%'] = array(
'title' => 'Awesome member',
'type' => MENU_NORMAL_ITEM,
'page callback' => 'aw_memberdata_fetch',
'delivery callback' => 'aw_memberdata_deliver',
'access arguments' => TRUE // this supposedly should allow anonymous access - from the web
);
return $items;
}
I have these two functions, one to fetch the data, one to show it:
function aw_memberdata_fetch(memberId)
{
//fetch array of objects from DB
...
return $items;
}
function aw_memberdata_deliver($items)
{
switch(arg(3)) //format, comes after items
{
case 'json':
drupal_json_output($items);
break;
default:
$output = makeHtml($items); //makes HTML
drupal_deliver_html_page($output);
}
}
With an authenticated browser, this works as intented. From a browser with no auth cookie I'm allowed to see the HTML, but the results are not there, it's like the page callback function doesn't run for anon users, but delivery callback does.
How do I make this work for anon users?
You are using access arguments with the default user_access function for permission verification. This will basically call user_access(true) which will return
true for authenticated user (you are probably testing with the administrator account (uid = 1)) which bypasses the verification check. Administrator has full privileges.
false for anonymous users because the string value of the first argument passed to the user_access true doesn't exist as a permission setting.
You should use either use
a custom YOURMODULE_access hook which does the permission verification
declare custom permissions for your module (YOURMODULE_permission hook)
don't use permission verification by using 'access callback' => true which gives access to anonymous and authenticated users
function awesome_module_menu() {
$items['awesome/member/%'] = array(
'title' => 'Awesome member',
'type' => MENU_NORMAL_ITEM,
'page callback' => 'aw_memberdata_fetch',
'delivery callback' => 'aw_memberdata_deliver',
'access callback' => true,
);
return $items;
}