When the user registers an account in my web application I would like for them to be added to the security group identified users so they have the necessary permissions to run my web application. This is what I've tried.
using SenseNet.ContentRepository.Storage;
using SenseNet.ContentRepository.Storage.Security;
namespace DerAssistantService.Actions
{
public static class UserActions
{
[ODataAction]
public static Content RegisterUser(Content content, string email, string password)
{
if (string.IsNullOrEmpty(email))
throw new ArgumentNullException(nameof(email));
if (string.IsNullOrEmpty(password))
throw new ArgumentNullException(nameof(password));
var username = email.Split('@').First();
using (new SystemAccount())
{
var user = Content.CreateNew("User", content.ContentHandler, username);
user["FullName"] = username;
user["Email"] = email;
user["LoginName"] = email;
user["Enabled"] = true;
user["Password"] = password;
user.Save();
var identifiedUsers = Node.Load<Group>("/Root/IMS/BuiltIn/Portal/IdentifiedUsers");
identifiedUsers.AddMember(user); // Error because type Content is not of type IGroup
return user;
}
}
}
}
The AddMember
method of the group class expects either an IUser
or an IGroup
instance. The user you created previously is of the Content
type, which is a wrapper type sensenet uses for everything. The underlying business object sits inside that content object, you can extract it using the ContentHandler
property:
identifiedUsers.AddMember(user.ContentHandler as IUser);
The Content object represents the upper, generic API layer where you find fields for example. The lower layer, accessible by the ContentHandler property represents the business layer with strongly typed classes like User
, File
or Workspace
.