Esempio n. 1
0
        public static UserCondition ToAppModel(ServiceData.Models.UserCondition given, bool includeOwner)
        {
            UserCondition cond = new UserCondition
            {
                Id         = given.Id,
                Passcode   = given.Passcode,
                StartDate  = given.StartDate,
                Treatment  = given.Treatment,
                Condition  = given.Condition,
                Finished   = given.Finished,
                SkinRegion = SkinRegion.ToAppModel(given.SkinRegion, true),
                Photos     = new List <Photo>()
            };

            if (includeOwner && given.Owner != null)
            {
                cond.Owner = User.ToAppModel(given.Owner);
            }

            if (given.Photos == null)
            {
                return(cond);
            }

            List <ServiceData.Models.Photo> photos = given.Photos.ToList();

            foreach (ServiceData.Models.Photo p in photos)
            {
                cond.Photos.Add(Photo.ToAppModel(p, false));
            }

            return(cond);
        }
        // DELETE api/values/5
        public async Task <HttpResponseMessage> Delete(int id)
        {
            try
            {
                ServiceData.Models.UserCondition found = _conditionRepository.GetById(id);
                if (found == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }
                if (found.Owner.Email != User.Identity.Name)
                {
                    return(Request.CreateResponse(HttpStatusCode.Forbidden));
                }

                await Delete(_conditionRepository, _shareRepository, _photoRepository, id);

                ServerUtils.LogTelemetryEvent(User.Identity.Name, "DeleteCondition");
                PostLog("UserConditions_Delete", found.Owner.Id);
                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (Exception e)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError));
            }
        }
        public async Task <HttpResponseMessage> ResetPin(int id)
        {
            ServiceData.Models.UserCondition found = _conditionRepository.GetById(id);

            if (found == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }
            if (found.Owner.Email != User.Identity.Name)
            {
                return(Request.CreateResponse(HttpStatusCode.Forbidden));
            }

            found.Passcode = rnd.Next(1000, 9999);

            ServiceData.Models.UserCondition final = _conditionRepository.Update(found);
            await ServerUtils.SendEmail(found.Owner.Email, found.Owner.Name,
                                        "Folder PIN Reset",
                                        string.Format("Hi {0},<br/>Your new PIN for the folder '{1}' has been reset, and is now '{2}'. Feel free to change it in the application.",
                                                      found.Owner.Name, found.Condition, final.Passcode));

            ServerUtils.LogTelemetryEvent(User.Identity.Name, "ResetConditionPIN");
            PostLog("UserConditions_PINReset", found.Owner.Id);
            return(Request.CreateResponse(HttpStatusCode.OK, Models.UserCondition.ToAppModel(final, true)));
        }
        // PUT api/values/5
        public HttpResponseMessage Put(int id, [FromBody] Models.UserCondition updated)
        {
            updated.Id = id;

            ServiceData.Models.UserCondition found = _conditionRepository.GetById(id);
            if (found == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }
            if (found.Owner.Email != User.Identity.Name)
            {
                return(Request.CreateResponse(HttpStatusCode.Forbidden));
            }

            ServiceData.Models.UserCondition final = _conditionRepository.Update(Models.UserCondition.ToServiceModel(updated, false));

            if (final == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            ServerUtils.LogTelemetryEvent(User.Identity.Name, "UpdateCondition");
            PostLog("UserConditions_Update", found.Owner.Id);
            return(Request.CreateResponse(HttpStatusCode.OK, Models.UserCondition.ToAppModel(final, true)));
        }
        public HttpResponseMessage Get(int id)
        {
            ServiceData.Models.UserCondition found = _conditionRepository.GetById(id);

            if (found == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            if (found.Owner.Email != User.Identity.Name)
            {
                return(Request.CreateResponse(HttpStatusCode.Forbidden));
            }

            if (found.Photos.Count() > 0)
            {
                found.Photos = found.Photos.OrderByDescending(photo => photo.CreatedAt);
            }

            Models.UserCondition toRet = Models.UserCondition.ToAppModel(found, false);

            ServerUtils.LogTelemetryEvent(User.Identity.Name, "GetCondition");
            PostLog("UserConditions_GetSingle", found.Owner.Id);
            return(Request.CreateResponse(HttpStatusCode.OK, toRet));
        }
Esempio n. 6
0
        private bool IsSharedOrOwned(ServiceData.Models.UserCondition cond)
        {
            if (cond.Owner.Email == User.Identity.Name)
            {
                return(true);
            }
            IReadWriteRepository <ServiceData.Models.Share> _shareRepository = new ShareRepository();

            return(_shareRepository.Search(s => s.UserCondition.Id == cond.Id &&
                                           s.SharedEmail == User.Identity.Name &&
                                           s.ExpireDate > DateTime.UtcNow).Any());
        }
Esempio n. 7
0
        public async Task <HttpResponseMessage> Get(string imageId, bool thumb = false)
        {
            int id;

            if (string.IsNullOrEmpty(imageId) || !Int32.TryParse(imageId, out id))
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            ServiceData.Models.Photo found = _photoRepository.GetById(id);
            if (found == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            ServiceData.Models.UserCondition foundCond = _conditionRepository.GetById(found.UserCondition.Id);
            if (foundCond.Owner.Email != User.Identity.Name)
            {
                return(Request.CreateResponse(HttpStatusCode.Forbidden));
            }

            string target = (thumb) ? found.ThumbUrl : found.Url;

            CloudBlobContainer container = await GetBlobContainer();

            Stream    blobStream = new MemoryStream();
            CloudBlob photoBlob  = container.GetBlobReference(target.Replace(ConfidentialData.BlobStorageUrl, ""));

            KeyVaultKeyResolver cloudResolver = new KeyVaultKeyResolver(ServerUtils.GetToken);
            IKey rsa = await cloudResolver.ResolveKeyAsync(ConfidentialData.KeyLocation, CancellationToken.None);

            BlobEncryptionPolicy policy  = new BlobEncryptionPolicy(null, cloudResolver);
            BlobRequestOptions   options = new BlobRequestOptions()
            {
                EncryptionPolicy = policy
            };

            await photoBlob.DownloadToStreamAsync(blobStream, null, options, null);

            blobStream.Position = 0;

            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);

            response.Content = new StreamContent(blobStream);
            response.Content.Headers.ContentDisposition          = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
            response.Content.Headers.ContentDisposition.FileName = imageId + Path.GetExtension(target);

            string eventName = thumb ? "DownloadThumb" : "DownloadImage";

            ServerUtils.LogTelemetryEvent(User.Identity.Name, eventName);

            return(response);
        }
Esempio n. 8
0
        public async Task <ActionResult> Download(string imageId, bool thumb = false)
        {
            int id;

            if (string.IsNullOrEmpty(imageId) || !Int32.TryParse(imageId, out id))
            {
                return(new HttpUnauthorizedResult());
            }

            IReadWriteRepository <ServiceData.Models.Photo>         _photoRepository = new PhotoRepository();
            IReadWriteRepository <ServiceData.Models.UserCondition> _condRepository  = new UserConditionsRepository();

            ServiceData.Models.Photo found = _photoRepository.GetById(id);
            if (found == null)
            {
                return(new HttpNotFoundResult());
            }

            ServiceData.Models.UserCondition foundCond = _condRepository.GetById(found.UserCondition.Id);
            if (!IsSharedOrOwned(foundCond))
            {
                return(new HttpUnauthorizedResult());
            }

            string target = (thumb) ? found.ThumbUrl : found.Url;

            CloudBlobContainer container = await UploadController.GetBlobContainer();

            Stream    blobStream = new MemoryStream();
            CloudBlob photoBlob  = container.GetBlobReference(target.Replace(ConfidentialData.BlobStorageUrl, ""));

            KeyVaultKeyResolver cloudResolver = new KeyVaultKeyResolver(ServerUtils.GetToken);
            IKey rsa = await cloudResolver.ResolveKeyAsync(ConfidentialData.KeyLocation, CancellationToken.None);

            BlobEncryptionPolicy policy  = new BlobEncryptionPolicy(null, cloudResolver);
            BlobRequestOptions   options = new BlobRequestOptions()
            {
                EncryptionPolicy = policy
            };

            await photoBlob.DownloadToStreamAsync(blobStream, null, options, null);

            blobStream.Position = 0;

            return(File(blobStream, "image/jpeg"));
        }
        // POST api/values
        public HttpResponseMessage Post([FromBody] Models.UserCondition newCondition)
        {
            try
            {
                newCondition.Owner = Models.User.ToAppModel(_userRepository.Search(u => u.Email == User.Identity.Name).FirstOrDefault());

                ServiceData.Models.UserCondition returned = _conditionRepository.Insert(Models.UserCondition.ToServiceModel(newCondition, true));

                ServerUtils.LogTelemetryEvent(User.Identity.Name, "AddCondition");
                PostLog("UserConditions_Create", newCondition.Owner.Id);
                return(Request.CreateResponse(HttpStatusCode.OK, Models.UserCondition.ToAppModel(returned, false)));
            }
            catch (Exception e)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, e));
            }
        }
Esempio n. 10
0
        // GET: Conditions
        public async Task <ActionResult> Index(int id)
        {
            await LoadViewBag();

            IReadWriteRepository <ServiceData.Models.UserCondition> _condRepository = new UserConditionsRepository();

            ServiceData.Models.UserCondition found = _condRepository.GetById(id);

            if (found == null)
            {
                return(new HttpNotFoundResult());
            }

            IReadWriteRepository <ServiceData.Models.Share> _shareRepository = new ShareRepository();

            ServiceData.Models.Share sh = _shareRepository.Search(s => s.UserCondition.Id == id &&
                                                                  s.SharedEmail == User.Identity.Name &&
                                                                  s.ExpireDate > DateTime.UtcNow).FirstOrDefault();

            if (found.Owner.Email != User.Identity.Name && sh == null)
            {
                return(new HttpUnauthorizedResult());
            }

            // Has been shared with the user (potentially themself but meh)
            if (sh != null)
            {
                sh.Updated = false;
                _shareRepository.Update(sh);

                ViewData["Title"] = string.Format("{0}'s {1}", found.Owner.Name, found.Condition);
            }
            else
            {
                ViewData["Title"] = found.Condition;
            }

            Models.UserCondition cond = Models.UserCondition.ToAppModel(found, true);

            ViewData["Condition"] = cond;

            return(View(cond.Photos));
        }
        public static async Task Delete(IReadWriteRepository <ServiceData.Models.UserCondition> conditionRep, IReadWriteRepository <ServiceData.Models.Share> shareRep, IReadWriteRepository <ServiceData.Models.Photo> photoRep, int id)
        {
            ServiceData.Models.UserCondition found = conditionRep.GetById(id);
            if (found == null)
            {
                return;
            }

            ServiceData.Models.Share[] foundShares = shareRep.Search(sh => sh.UserCondition.Id == found.Id).ToArray();
            foreach (ServiceData.Models.Share share in foundShares)
            {
                await shareRep.Delete(share.Id);
            }

            CloudBlobContainer container = await UploadController.GetBlobContainer();

            foreach (ServiceData.Models.Photo photo in found.Photos)
            {
                await PhotoController.Delete(photoRep, photo.Id);
            }

            await conditionRep.Delete(id);
        }
Esempio n. 12
0
        private bool IsSameUser(ServiceData.Models.Photo foundPhoto)
        {
            ServiceData.Models.UserCondition foundCond = _conditionRepository.GetById(foundPhoto.UserCondition.Id);

            return(foundCond.Owner.Email == User.Identity.Name);
        }
Esempio n. 13
0
        public async Task <HttpResponseMessage> Get()
        {
            try
            {
                string[] testUsers = new string[]
                {
                    "*****@*****.**",
                    "*****@*****.**",
                    "*****@*****.**",
                    "*****@*****.**",
                    "*****@*****.**",
                    "*****@*****.**",
                    "*****@*****.**",
                    "*****@*****.**",
                    "*****@*****.**"
                };

                string baseFileLoc = "C:/Users/tgs03_000/Downloads/SkinSelfies";

                //if (Directory.Exists(baseFileLoc))
                //{
                //    Directory.Delete(baseFileLoc, true);
                //}

                if (!Directory.Exists(baseFileLoc))
                {
                    Directory.CreateDirectory(baseFileLoc);
                }

                List <Task>   allTasks   = new List <Task>();
                List <string> nullEmails = new List <string>();

                foreach (string userEmail in testUsers)
                {
                    ServiceData.Models.User user = _userRepository.Search(u => u.Email == userEmail).FirstOrDefault();

                    DirectoryInfo userDir = Directory.CreateDirectory(Path.Combine(baseFileLoc, userEmail));

                    if (user == null)
                    {
                        nullEmails.Add(userEmail);
                        continue;
                    }

                    AboutUser userDetails = new AboutUser
                    {
                        Id    = user.Id,
                        Email = user.Email,
                        Name  = user.Name,
                        Dob   = user.BirthDate
                    };

                    File.WriteAllText(Path.Combine(userDir.FullName, "AboutUser.txt"), JsonConvert.SerializeObject(userDetails, Formatting.Indented));

                    foreach (ServiceData.Models.UserCondition cond in user.Conditions)
                    {
                        ServiceData.Models.UserCondition fullCond = _conditionRepository.GetById(cond.Id);

                        string condPath = Path.Combine(userDir.FullName, cond.Id.ToString());

                        DirectoryInfo condDir = Directory.Exists(condPath)? new DirectoryInfo(condPath) : Directory.CreateDirectory(condPath);

                        AboutCondition condDetails = new AboutCondition
                        {
                            Id         = user.Id,
                            Name       = cond.Condition,
                            SkinRegion = fullCond.SkinRegion.BodyPart.Name + " - " + fullCond.SkinRegion.Name,
                            StartDate  = cond.StartDate,
                            NumPhotos  = fullCond.Photos.Count()
                        };

                        File.WriteAllText(Path.Combine(condDir.FullName, "AboutCondition.txt"), JsonConvert.SerializeObject(condDetails, Formatting.Indented));

                        foreach (ServiceData.Models.Photo photo in fullCond.Photos)
                        {
                            string filename = Path.Combine(condDir.FullName, photo.CreatedAt.ToString("yyyy-MM-dd-HH-mm-ss.") + Path.GetExtension(photo.Url));

                            if (File.Exists(filename))
                            {
                                continue;
                            }

                            CloudBlobContainer container = await GetBlobContainer();

                            Stream    blobStream = new MemoryStream();
                            CloudBlob photoBlob  = container.GetBlobReference(photo.Url.Replace(ConfidentialData.BlobStorageUrl, ""));

                            KeyVaultKeyResolver cloudResolver = new KeyVaultKeyResolver(ServerUtils.GetToken);
                            IKey rsa = await cloudResolver.ResolveKeyAsync(ConfidentialData.KeyLocation, CancellationToken.None);

                            BlobEncryptionPolicy policy  = new BlobEncryptionPolicy(null, cloudResolver);
                            BlobRequestOptions   options = new BlobRequestOptions()
                            {
                                EncryptionPolicy = policy
                            };

                            await photoBlob.DownloadToStreamAsync(blobStream, null, options, null);

                            blobStream.Position = 0;

                            using (var fileStream = File.Create(filename))
                            {
                                await blobStream.CopyToAsync(fileStream);
                            }
                        }
                    }
                }

                string nullString = "";

                foreach (string nEmail in nullEmails)
                {
                    nullString += nEmail + ", ";
                }

                return(Request.CreateResponse(HttpStatusCode.OK, "Files located at " + baseFileLoc + " Null emails: " + nullString));
            }
            catch (Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
            }
        }