Username with System.User - windows-store-apps

Today I wanted to greet the user in my app by name, but I did not manage to get it.
I found System.User, but lacking some examples I did not manage to get the info I needed. I saw no possibility to get the current user (id) to call User.GetFromId().
Can you guide me into the right direction? Have I been on the wrong path?

Okay, So first things first, getting access to a user's personal information is a privilege you have to request, so in your store app's Package.appxmanifest, you'll need to enable the User Account Information capability in the Capabilities tab.
Next, you'll want to be using the Windows.System.User class, not the System.User (System.User isn't available to Windows store apps, which you appear to be discussing given the tags you provided for your question)
Third, you'll want to request personal information like this.
IReadOnlyList<User> users = await User.FindAllAsync(UserType.LocalUser, UserAuthenticationStatus.LocallyAuthenticated);
User user = users.FirstOrDefault();
if (user != null)
{
String[] desiredProperties = new String[]
{
KnownUserProperties.FirstName,
KnownUserProperties.LastName,
KnownUserProperties.ProviderName,
KnownUserProperties.AccountName,
KnownUserProperties.GuestHost,
KnownUserProperties.PrincipalName,
KnownUserProperties.DomainName,
KnownUserProperties.SessionInitiationProtocolUri,
};
IPropertySet values = await user.GetPropertiesAsync(desiredProperties);
foreach (String property in desiredProperties)
{
string result;
result = property + ": " + values[property] + "\n";
System.Diagnostics.Debug.WriteLine(result);
}
}
When you call GetPropertiesAsync, your user will get a permission prompt from the system asking them if they want to give you access to that. If they answer 'No', you'll get an empty user object (but you'll still get a unique token you can use to distinguish that user if they use the app again).
If they answer yes, you'll be able to get access to the properties below, and various others.
See the UserInfo Sample Microsoft provided for more examples.

Related

Firebase web authenticate and add to database

I am stuck on this and the other posts I have read on here are not useful. So I've reached a point where i need to ask for help after many hours on not resolving what I feel should be a simple task. I program in Swift usually and really know little about html or javasript.
I am building a simple webpage to log-in to Firebase and a second linked page to upload data to a database. Both work fine. The problem is getting the uploaded data to link to the uid of the current user.
So I am logged into an existing user with it's own uid. How do I then upload the data to the current user did in the database? Should be simple but I am just not getting it :-(
Code for uploading data is as follows (note I have tried using both set and push):
// Generate a reference to a new location and add some data using push()
var postsRef = ref.child("users");
var newPostRef = postsRef.push({
// var newPostRef = postsRef.set({
name: _name,
property: _property,
email: _email,
phone: _phone,
Any help, or better still a working simple example would be useful. I have read the docs on Firebase, so please don't direct me there :-)
Many thanks in anticipation
It is a best practice to create a new database node using the UID generated by the account creation as the path after /users.
Right now, when you push data into /users, Firebase creates a uid for that particular array item that does not correspond to the UID of the user.
If you use set, you need to specify the path you will set which should include the long UI: /users/longGUIDhere
You can get the user id with something like this (from Firebase docs):
var user = firebase.auth().currentUser;
var name, email, photoUrl, uid;
if (user != null) {
name = user.displayName;
email = user.email;
photoUrl = user.photoURL;
uid = user.uid; // The user's ID, unique to the Firebase project. Do NOT use
// this value to authenticate with your backend server, if
// you have one. Use User.getToken() instead.
}
And then you shouuld use uid to populate the path like below to save their info:
function writeUserData(userId, name, email, imageUrl) {
firebase.database().ref('users/' + userId).set({
username: name,
email: email,
profile_picture : imageUrl
});
}
I know you asked not to be referred to the Firebase docs, but it also looks like you are using an older version of the SDK, so that could be part the issue as well. I recommend taking a look at these two page, since that is where I pulled these verbatim examples:
https://firebase.google.com/docs/database/web/read-and-write
https://firebase.google.com/docs/auth/web/manage-users
var postsRef = ref.child("users/current user id or json key");
this will help you to update the details of current user.

EWS SearchFolder does not return values from body

I am trying to create a SearchFolder using the EWS API (managed or web service directly). I noticed that I if I create a SearchFilter.ContainsSubstring on the ItemSchema.Body, I do not get any conversations from it.
here is how I create my folder:
var folder = new SearchFolder(service)
{
DisplayName = topic
};
var searchParameters = folder.SearchParameters;
searchParameters.SearchFilter = new SearchFilter.ContainsSubstring(ItemSchema.Body, topic, ContainmentMode.Substring, ComparisonMode.IgnoreCaseAndNonSpacingCharacters);
searchParameters.RootFolderIds.Add(WellKnownFolderName.Root);
searchParameters.Traversal = SearchFolderTraversal.Deep;
folder.Save(WellKnownFolderName.SearchFolders);
Later, I try to get the conversations from this folder:
service.FindConversation(conversationView, folder.Id);
And this returns 0 conversations.
I made sure by sending two messages to my email account, the first with a special term only in the subject, and the second with the same term in the body. If I create a SearchFolder with a filter on the ItemSchema.Subject, I get the first conversation, but using the SearchFolder I created above, I do not get the expected result.
Are there some restrictions regarding the ContainsSubstring SearchFilter? I tried using NormalizedBody or TextBody, but then I got errors in the folder creation process. Is there anything else I am missing?
Doing a search filter on the body will likely be problematic. This goes back to how potentially large properties like Body are handled in contents tables. A query string search would likely work better, but you can't use a query string to create a search folder.

Is HTML5 localstorage appropriate to store input field values?

I have a question, on how to best store local values of some form fields.
In my website, users use the keypad to keep a tally count of items. They can enter a label for the items they count. The problem is that each user apply different labels for their needs - and, each time they visit the labels are blank.
My sites are running through site44.com, which does not allow the use of server side php. So, in my research, I think using HTML5 localstorage may allow a user to keep the label after the exit the site?
Is this a correct interpretation?
Can someone give me a guide if I have, say 3 inputs - with different ids - how to set up the script?
you can use the local storage like this :
var fn = document.getElementById("firstname").value;
localStorage.setItem("firstname", fn);
var ln = document.getElementById("lastname").value;
localStorage.setItem("lastname", ln);
var em = document.getElementById("email").value;
localStorage.setItem("email", em);
thus the clients browser will have these items set in their local storage.
Now if a user visits the website afterwards. you can check for the value of localStorage and find the items of your need.
Suppose on users' next visit you want to send him a greet message ( he has not logged in ofcourse ) you can use a script like this below:
var name = localStorage.getItem("firstname");
alert("Hello"+name);

Laravel Eloquent how to limit access to logged in user only

I have a small app where users create things that are assigned to them.
There are multiple users but all the things are in the same table.
I show the things belonging to a user by retrieving all the things with that user's id but nothing would prevent a user to see another user's things by manually typing the thing's ID in the URL.
Also when a user wants to create a new thing, I have a validation rule set to unique but obviously if someone else has a thing with the same name, that's not going to work.
Is there a way in my Eloquent Model to specify that all interactions should only be allowed for things belonging to the logged in user?
This would mean that when a user tries to go to /thing/edit and that he doesn't own that thing he would get an error message.
The best way to do this would be to check that a "thing" belongs to a user in the controller for the "thing".
For example, in the controller, you could do this:
// Assumes that the controller receives $thing_id from the route.
$thing = Things::find($thing_id); // Or how ever you retrieve the requested thing.
// Assumes that you have a 'user_id' column in your "things" table.
if( $thing->user_id == Auth::user()->id ) {
//Thing belongs to the user, display thing.
} else {
// Thing does not belong to the current user, display error.
}
The same could also be accomplished using relational tables.
// Get the thing based on current user, and a thing id
// from somewhere, possibly passed through route.
// This assumes that the controller receives $thing_id from the route.
$thing = Users::find(Auth::user()->id)->things()->where('id', '=', $thing_id)->first();
if( $thing ) {
// Display Thing
} else {
// Display access denied error.
}
The 3rd Option:
// Same as the second option, but with firstOrFail().
$thing = Users::find(Auth::user()->id)->things()->where('id', '=', $thing_id)->firstOrFail();
// No if statement is needed, as the app will throw a 404 error
// (or exception if errors are on)
Correct me if I am wrong, I am still a novice with laravel myself. But I believe this is what you are looking to do. I can't help all that much more without seeing the code for your "thing", the "thing" route, or the "thing" controller or how your "thing" model is setup using eloquent (if you use eloquent).
I think the functionality you're looking for can be achieved using Authority (this package is based off of the rails CanCan gem by Ryan Bates): https://github.com/machuga/authority-l4.
First, you'll need to define your authority rules (see the examples in the docs) and then you can add filters to specific routes that have an id in them (edit, show, destroy) and inside the filter you can check your authority permissions to determine if the current user should be able to access the resource in question.

CRM 4.0 - Is there a way to know if a contact from a specific marketing list has respond to a campaign?

I am wondering how can I do the following about MS CRM 4.0:
I want to know for a campaign if a contact from a specific marketing list has not replied yet.
The field custom in the campaign response form is a partyfield. CRM doesn’t allow a PartyList field to be queried using a QueryExpression
Any ideas?
Thanks,
Katya
You cannot retrieve activityparty records directly, but you can use them in LinkEntities:
private bool contactHasResponded(Guid idCampaign, Guid idContact)
{
QueryExpression qryCampaignResponses = new QueryExpression("campaignresponse");
qryCampaignResponses.ColumnSet = new AllColumns();
qryCampaignResponses.Criteria = new FilterExpression();
qryCampaignResponses.Criteria.AddCondition("regardingobjectid", ConditionOperator.Equal, idCampaign);
LinkEntity leContact = new LinkEntity("campaignresponse", "activityparty", "activityid", "activityid", JoinOperator.Inner);
leContact.LinkCriteria = new FilterExpression();
leContact.LinkCriteria.AddCondition("partyid", ConditionOperator.Equal, idContact);
qryCampaignResponses.LinkEntities.Add(leContact);
List<gcCampaignresponse> lstCampaignResponses = gcCampaignresponse.RetrieveMultiple(m_svcCrm, qryCampaignResponses);
return (lstCampaignResponses.Count > 0);
}
This will tell you whether there's a campaign response for a given campaign and contact. (I use entity classes generated by Stunnware Tools, so the RetrieveMultiple call looks a little different, but I think you get my point).
If you turn this QueryExpression/LinkEntity construct upside down, you can also get all contacts that have responded to a given campaign (you can also restrict that to contacts in a certain marketing list through a second LinkEntity).
The only thing that's not possible directly with a single query is the "negative" check you are looking for, so you'll have to take this result and do an "outer join" against your marketing list to get the contacts that have not responded.