Skip to content

Instantly share code, notes, and snippets.

@cburnette
Created July 14, 2016 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cburnette/bf4fc26dce1ccd719b7cc2029c566d98 to your computer and use it in GitHub Desktop.
Save cburnette/bf4fc26dce1ccd719b7cc2029c566d98 to your computer and use it in GitHub Desktop.
public static async Task PrepareBoxAppUser(ClaimsIdentity externalIdentity)
{
var auth0UserId = externalIdentity.Claims.FirstOrDefault(c => c.Type == "user_id").Value;
var boxId = GetBoxIdFromAuth0(auth0UserId);
if (boxId == null)
{
//create a new app user in Box
string email = externalIdentity.Claims.FirstOrDefault(c => c.Type == "email").Value;
string name = email;
if (email.Equals("testclient1@example.com"))
{
name = "Test Client1";
}
else if(email.Equals("testclient2@example.com"))
{
name = "Test Client2";
}
var userRequest = new BoxUserRequest() { Name = name, IsPlatformAccessOnly = true };
var appUser = await AdminClient().UsersManager.CreateEnterpriseUserAsync(userRequest);
boxId = appUser.Id;
//store the boxId in the user's Auth0 metadata
var meta = new Dictionary<string,object>();
meta.Add(BOX_USER_ID_KEY,boxId);
AUTH0_CLIENT.UpdateUserMetadata(auth0UserId, meta);
//now do the initial box account setup
var boxClient = UserClient((string)boxId);
var folderRequest = new BoxFolderRequest() { Name = name + " Docs", Parent = new BoxRequestEntity() { Id = "0" } };
var sharedFolder = await boxClient.FoldersManager.CreateAsync(folderRequest);
folderRequest = new BoxFolderRequest() { Name = "Private", Parent = new BoxRequestEntity() { Id = "0" } };
var privateFolder = await boxClient.FoldersManager.CreateAsync(folderRequest);
//Create Groups
var bgr = new BoxGroupRequest() { Name = name + " Advisor Group" };
var group = await AdminClient().GroupsManager.CreateAsync(bgr);
//Create Colaboration
var bcur = new BoxCollaborationUserRequest() { Id = group.Id, Type = BoxType.group };
var bcr = new BoxCollaborationRequest()
{
AccessibleBy = bcur,
Item = new BoxRequestEntity() { Id = sharedFolder.Id, Type = BoxType.folder },
Role = "viewer uploader"
};
var colaboration = await boxClient.CollaborationsManager.AddCollaborationAsync(bcr);
//Add Advisor 1 to Group
var bgr2 = new BoxGroupRequest() { Id= group.Id };
var bre = new BoxRequestEntity() { Id = advisor1id };
var gmr = new BoxGroupMembershipRequest() { Group = bgr2, User = bre };
var membership = await AdminClient().GroupsManager.AddMemberToGroupAsync(gmr);
//var pathToFile = HttpContext.Current.Server.MapPath("~/Assets/");
//var fileName = "test.txt";
//using (FileStream fs = File.Open(pathToFile + fileName, FileMode.Open))
//{
// // Create request object with name and parent folder the file should be uploaded to
// BoxFileRequest request = new BoxFileRequest()
// {
// Name = fileName,
// Parent = new BoxRequestEntity() { Id = "0" }
// };
// var boxFile = await boxClient.FilesManager.UploadAsync(request, fs);
//}
}
HttpContext.Current.Session[BOX_USER_ID_KEY] = (string)boxId;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment