Get UserId from the Session Id and Server URL - salesforce

I have a requirement, where I will be getting the Session Id and Server URL(parameters of a webservice method).
Say like this:
Session ID : 00D900000xxxxxx!ARUAQOb4VVoQR1UXlY_Hvuy1DdKdN6nSfnNJKYwPTF9R3tYuA2jzBsWXHIGDQUFL13iebnYSDKKC45H98TzVxxxxxxxxxx
Server URL :
https://ap1.salesforce.com/services/Soap/u/12.0/00D900000xxxxxx
Now i need to get the User Id(or any other user details) from these two.
Thanks in Advance!!
Nitin

You could use the SOAP API, which has a GetUserInfo method. Calling this method will return a GetUserInfoResult object, which will contain the user ID associated to the session.

Adam, GetUserInfo method requires existing connection object which we can create by calling method, so I applied the following code and now everything works fine!
ConnectorConfig config = new ConnectorConfig();
config.setAuthEndpoint("https://login.salesforce.com/services/Soap/c/24.0/");
config.setServiceEndpoint("https://na14.salesforce.com/services/Soap/c/24.0/00DXXXXXXXXXXXX");
config.setSessionId("00DXXXXXXXXXXXX!1AQ4AQO980Fmu25SOFQxxOlQN8zAaHOlnfdk._rZU2Vkf_CV0HJREqKavMLaPg9jtA9N517MNHLdLeF.aVkoZtnk2eu7u.XNn");
connection = new EnterpriseConnection(config); GetUserInfoResult
userInfo = connection.getUserInfo();
I used the same server URL and sessionId that I have received from query parameter string.

Related

Dynamic named credentials Salesforce

I need to update the permissions of a Drive document from Salesforce.
I wanted to use Named Credentials, but I didn't find any way of building a call like this one:
https://www.googleapis.com/drive/v3/files/{documentId}/permissions
where {documentId} is a dynamic value.
I've seen that it is possible to add a prefix, but actually even if I create a Named Credential with only https://www.googleapis.com/drive/v3/files, when I call it from my Apex class I get a permission error.
Is there a way to achieve what I would like or I need to change approach?
Thank you
What exactly error are you getting? Salesforce security about not having access to class X? Something about callouts not allowed from triggers? You're sure it works with hardcoded document id?
Should be possible to make the named credential point to https://www.googleapis.com or https://www.googleapis.com/drive/v3/files/ and then add the rest of the endpoint in Apex. If it throws errors - maybe Drive's API is special, you'd need to read up.
String endpoint = 'callout:MyNamedCredential' + '/abc123/permissions';
HttpRequest req = new HttpRequest();
req.setEndpoint(endpoint);
req.setMethod('GET');
I have something like that:
Named credential pointing to https://maps.googleapis.com/maps/api/geocode/json
and then
static final String ENDPOINT = 'callout:GoogleMaps?key={0}&latlng={1}&result_type=premise%7Cstreet_address';
String apiKey = SomeCustomSetting__c.getInstance().GoogleApiKey__c;
String latLng = '60.23,11.17';
req.setEndpoint(String.format(ENDPOINT, new List<String>{apiKey, latLng}));
HttpResponse res = h.send(req);
You could also look into "Files Connect" API I guess.

Persisting a backbone model and the id

Let's assume we create a new model class an instanciate a person using it:
var User = Backbone.Model.extend({
urlRoot: '/user'
});
var nou = new User({
name: "nourdine"
});
Now, of course, we want to persist it. Not having added an id backbone will create a POST request and communicate the server the intention to create an entity under /user containing the data {name: "nourdine"}. Here's how we do it:
nou.save(null, {
success: function (model, response, options) {
// ... what do I do here?
}
})
The server will now create a record in the db containing the JSON data rearranged in some form and assign an ID to it. NOW:
1 - What is the server suppose to return in the HTTP response? A JSON containing the JSON provided by the clinet + the newly created fields, namely the ID of the new record?
2 - who is going to update the model in the client with these data? Me? Matter of fact I would like to tell the model in the client that a new ID has been assign to it by the server so that the next time I do user.save() I will obtain a PUT rather than a POST. But who is supposed to update the model in the client?
Thanks
so this is my work flow for this
client -> create model and populate with data
client -> save model (model.save())
server -> create server side version of model using data, assign an id
server -> respond with success and the id of the newly created model
client -> in the success set the id to the one passed back
now the only potential issue i have with my work flow is if something did not get set in the server successfully but the model was still created, my client model would not reflect that of the server anymore, but i minimize this by returning error if the model could not be created exactly as passed.
And now i am able to call model.save() again this time having the id so initiating a PUT request
From the documentation to a Backbone.Model
After a successful server-side save, the client is (optionally) updated with the server-side state.
So if you return a valid JSON your model will be updated automatically

Perl CGI::Session, Multiple Sessions with same IDs, MySQL Driver

I have some problem with CGI::Session.
I try to create a new session with an existing session id passed with the cgi object. Normally the session should reuse the existing session in the database, but it doesn't. Instead it creates a new session database entry with the exact same session id.
Here are the relevant parts of my code:
CGI::Session->name("DCGISESSID");
$session = CGI::Session->new('driver:mysql', $cgi,
{
TableName=>'DSESSIONS',
IdColName=>'id',
DataColName=>'a_session',
Handle=>$dbh,
});
$sessioncookie = CGI::Cookie->new(-name=>'DCGISESSID', -value=>$session->id, -expires=>'+1h', -path=>'/');
The code works as long as I do not set the cookie name with the name() method and use the default value CGISESSID as cookiename. But for some reason, after changing it to DCGISESSID with CGI::Session->name("DCGISESSID"); it doesn't work.
Does someone got the same problem or has any advice for me?
Solved the problem. I configured the table false, that's why id wasn't a primary key too.

PUT/GET with Payload using Restangular

I am using Restangular in one of my works
The server guys have give me the following calls which i need to integrate on the AngularJS client
PUT api/partners/password – RequestPayload[{password,confirmpassword}]
partner id is being sent in the header
GET api/partners/password/forgot/ - Request Payload [{emailaddress}]
partner id is being sent in the header
The javascript code that I have written to call these services is as follow
Restangular.all('Partners').one('Password').put(params); - sends params as query string
Restangular.all('Partners').one('Password').one('Forgot').get(params); - sends object in the url
I have tried other ways but it simply doesn't make the correct call.
Help me out guys!
So, for point #1. it puts the object at hand, not another object. So you have 2 options:
Option 1
var passReq = Restangular.all('Partners').one('Password');
passReq.confirmPassword = ....
passReq.put(); // confirmPassword and the params of the object will be sent
Option 2 is
var passReq = Restangular.all('Partners').one('Password').customPUT(obj);
For Point #2, you cannot send a request body (payload) in the GET unfortunately.

DotNetNuke Retrieving UserInfo for the given UserID

Is there somthing in the dotnetnuke framework which will allow me to pass it a userId and it would return the UserInfo object filled with details of that userId.
If not what would be the normal way of doing this?
Try this (in DNN 5.x with C#)
private UserInfo _currentUser =
DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo();
Then use the UserInfo later...
int UserID = _currentUser.UserID
I used the way posted by bdukes with one modification: PortalId can be get from PortalSettings:
DotNetNuke.Entities.Users.UserInfo user = DotNetNuke.Entities.Users.UserController.GetUser(PortalSettings.PortalId, user_id, true);
To get the current user, as of version 7.3 all of the above have been deprecated. Now you need to use access the user info via the Instance property and the GetCurrentUserInfo() method, i.e.:
DotNetNuke.Entities.Users.UserController.Instance.GetCurrentUserInfo()
Hence you could get the UserId as so:
DotNetNuke.Entities.Users.UserController.Instance.GetCurrentUserInfo().UserID
So, given a user id, you could get the user's info like this:
UserController.GetUserById(PortalId,your_user_id)
Note that PortalId is a property provided by the DNN context, so you can simply type it as above.
I hope this helps.
I believe that DotNetNuke.Entities.Users.UserController has a method (GetUser) that will do that, if you also have a portal ID. Users can be shared across portals, so it's (apparently) necessary to know the portal for which you're requesting the user information before they can properly fill the UserInfo object.
If you only have a user ID and no portal ID, I'd first suggest that you see if you can get a portal ID, too. If not, you'll need to go to the database to get what you need. Ideally, you'll be in there as little as you can be (since the database isn't a guaranteed API). So, if you just do a quick query to get a portal ID for the user:
SELECT PortalID From {databaseOwner}{objectQualifier}UserPortals WHERE UserID = #userId
You can then use UserController.GetUser to retrieve what you need.
It's not return User ID what is the problem
Dim nowUser As UserInfo = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo
response.write(nowUser)
If you need to get the current user it's simpler:
Dim nowUser As UserInfo = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo
Just a note.

Resources