public async Task <HttpResponseMessage> EditUser(IncomingEditUser model)
        {
            return(await ErrorFactory.Handle(async() =>
            {
                var userID = User.Identity.GetUserId();

                if (userID == null)
                {
                    throw new Exception();
                }

                using (var unitOfWork = new UnitOfWork())
                {
                    unitOfWork.Users.EditUser(userID, model.PhoneNumber, model.FirstName, model.LastName);

                    unitOfWork.Complete();

                    try
                    {
                        if (model.Image != null && !string.IsNullOrWhiteSpace(model.Image.Data))
                        {
                            //This is added to help yogita with her base64 problems.
                            model.Image.Data = model.Image.Data.Replace(' ', '+');


                            var data = ImageFactory.ConvertBase64ToArray(model.Image.Data);

                            GalleryManager galMan = new GalleryManager();

                            var pictureId = await galMan.UploadImage(data, userID);

                            if (pictureId == null)
                            {
                                throw new Exception();
                            }

                            unitOfWork.Users.SetProfilePictureADMIN(userID, pictureId ?? Guid.NewGuid());

                            unitOfWork.Complete();
                        }
                    }
                    catch (Exception)
                    {
                        //Maybe try to delete image.
                    }


                    return GetInformationForCurrentUserResponseMessage(userID);
                }
            }, this.Request));
        }
Ejemplo n.º 2
0
        public async Task <HttpResponseMessage> AddIcon(IncomingBase64File model)
        {
            return(await ErrorFactory.Handle(async() =>
            {
                var userID = User.Identity.GetUserId();

                if (userID == null)
                {
                    throw new Exception();
                }

                using (var unitOfWork = new UnitOfWork())
                {
                    try
                    {
                        if (model != null && !string.IsNullOrWhiteSpace(model.Data))
                        {
                            //This is added to help yogita with her base64 problems.
                            model.Data = model.Data.Replace(' ', '+');


                            var data = ImageFactory.ConvertBase64ToArray(model.Data);

                            GalleryManager galMan = new GalleryManager();

                            var pictureId = await galMan.UploadImage(data, userID, ImageFormat.Png);

                            if (pictureId == null)
                            {
                                throw new Exception();
                            }

                            var icon = unitOfWork.Pictures.CreateIconLink(userID, pictureId ?? Guid.NewGuid());

                            unitOfWork.Complete();


                            return JsonFactory.CreateJsonMessage(OutgoingIcon.Parse(icon), HttpStatusCode.OK, this.Request);
                        }
                    }
                    catch (Exception)
                    {
                        //Maybe try to delete image.
                    }

                    return JsonFactory.CreateJsonMessage(new OutgoingMessage {
                        Action = "unknownError"
                    }, HttpStatusCode.InternalServerError, this.Request);
                }
            }, this.Request));
        }
        public async Task <HttpResponseMessage> RegisterExternal(IncomingExternalRegister model)
        {
            return(await ErrorFactory.Handle(async() =>
            {
                if (model == null || string.IsNullOrWhiteSpace(model.Provider) || string.IsNullOrWhiteSpace(model.ExternalToken))
                {
                    throw new InvalidModelException()
                    {
                        Action = "nullData"
                    };
                }

                var pwResult = await UserManager.PasswordValidator.ValidateAsync(model.Password);

                if (pwResult.Succeeded == false)
                {
                    throw new InvalidModelException()
                    {
                        Action = "invalidPassword"
                    };
                }

                var verifiedAccessToken = await VerifyExternalAccessToken(model.Provider, model.ExternalToken);
                if (verifiedAccessToken == null)
                {
                    throw new InvalidModelException()
                    {
                        Action = "failedVerification"
                    };
                }

                ApplicationUser user = await UserManager.FindAsync(new UserLoginInfo(model.Provider, verifiedAccessToken.user_id));

                bool hasRegistered = user != null;

                if (hasRegistered)
                {
                    throw new InvalidModelException()
                    {
                        Action = "alreadyRegistered"
                    };
                }

                var emailAlreadyExists = await UserManager.FindByEmailAsync(model.Email) != null;

                if (emailAlreadyExists)
                {
                    throw new InvalidModelException()
                    {
                        Action = "emailAlreadyRegistered"
                    };
                }

                user = new ApplicationUser()
                {
                    UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, PhoneNumber = model.PhoneNumber, DateCreated = DateTime.UtcNow, SitePermissionsId = Convert.ToInt32(SitePermissionsEnum.REGULAR_APP_USER)
                };

                IdentityResult result = await UserManager.CreateAsync(user, model.Password);
                if (!result.Succeeded)
                {
                    throw new Exception();
                }

                var info = new ExternalLoginInfo()
                {
                    DefaultUserName = model.Email,
                    Login = new UserLoginInfo(model.Provider, verifiedAccessToken.user_id)
                };

                result = await UserManager.AddLoginAsync(user.Id, info.Login);
                if (!result.Succeeded)
                {
                    try
                    {
                        await UserManager.DeleteAsync(user);
                    }
                    catch (Exception)
                    {
                    }

                    throw new Exception();
                }



                await SignInManager.SignInAsync(user, true, true);

                try
                {
                    if (model.Image != null && !string.IsNullOrWhiteSpace(model.Image.Data))
                    {
                        //This is added to help yogita with her base64 problems.
                        model.Image.Data = model.Image.Data.Replace(' ', '+');


                        var data = ImageFactory.ConvertBase64ToArray(model.Image.Data);

                        GalleryManager galMan = new GalleryManager();

                        var pictureId = await galMan.UploadImage(data, user.Id);

                        if (pictureId == null)
                        {
                            throw new Exception();
                        }

                        using (var unitOfWork = new UnitOfWork())
                        {
                            unitOfWork.Users.SetProfilePictureADMIN(user.Id, pictureId ?? Guid.NewGuid());

                            unitOfWork.Complete();
                        }
                    }
                }
                catch (Exception)
                {
                    //Maybe try to delete image.
                }

                await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);


                return GetInformationForCurrentUserResponseMessage(user.Id);
            }, this.Request));
        }
        public async Task <HttpResponseMessage> Register(IncomingRegister model)
        {
            return(await ErrorFactory.Handle(async() =>
            {
                var pwResult = await UserManager.PasswordValidator.ValidateAsync(model.Password);

                if (pwResult.Succeeded == false)
                {
                    return JsonFactory.CreateJsonMessage(new OutgoingMessage {
                        Message = "Registration failed", Action = "invalidPassword"
                    }, HttpStatusCode.Forbidden, this.Request);
                }

                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, PhoneNumber = model.PhoneNumber, FirstName = model.FirstName, LastName = model.LastName, DateCreated = DateTime.UtcNow, SitePermissionsId = Convert.ToInt32(SitePermissionsEnum.REGULAR_APP_USER)
                };
                var result = await UserManager.CreateAsync(user, model.Password);



                if (result.Succeeded)
                {
                    try
                    {
                        if (model.Image != null && !string.IsNullOrWhiteSpace(model.Image.Data))
                        {
                            //This is added to help yogita with her base64 problems.
                            model.Image.Data = model.Image.Data.Replace(' ', '+');


                            var data = ImageFactory.ConvertBase64ToArray(model.Image.Data);

                            GalleryManager galMan = new GalleryManager();

                            var pictureId = await galMan.UploadImage(data, user.Id);

                            if (pictureId == null)
                            {
                                throw new Exception();
                            }

                            using (var unitOfWork = new UnitOfWork())
                            {
                                unitOfWork.Users.SetProfilePictureADMIN(user.Id, pictureId ?? Guid.NewGuid());

                                unitOfWork.Complete();
                            }
                        }
                    }
                    catch (Exception)
                    {
                        //Maybe try to delete image.
                    }

                    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);


                    return GetInformationForCurrentUserResponseMessage(user.Id);
                }

                return JsonFactory.CreateJsonMessage(new OutgoingMessage {
                    Message = "Registration failed", Action = "unknownFailure"
                }, HttpStatusCode.Forbidden, this.Request);
            }, this.Request));
        }
Ejemplo n.º 5
0
        public async Task <HttpResponseMessage> EditHotel(IncomingEditHotel hotel)
        {
            return(await ErrorFactory.Handle(async() =>
            {
                var userId = User?.Identity?.GetUserId();

                if (userId == null)
                {
                    throw new Exception("User not found.");
                }

                if (hotel == null)
                {
                    throw new InvalidModelException("hotel cannot be null");
                }

                if (hotel.TaxRate < 0 || hotel.TaxRate > 1)
                {
                    throw new InvalidModelException("Tax rate must be greater than 0 and less than 1");
                }

                using (var unitOfWork = new UnitOfWork())
                {
                    var finHotel = unitOfWork.Hotels.EditHotel(userId, hotel);

                    unitOfWork.Complete();

                    try
                    {
                        if (hotel.Image != null && !string.IsNullOrWhiteSpace(hotel.Image.Data))
                        {
                            //This is added to help yogita with her base64 problems.
                            hotel.Image.Data = hotel.Image.Data.Replace(' ', '+');


                            var data = ImageFactory.ConvertBase64ToArray(hotel.Image.Data);

                            GalleryManager galMan = new GalleryManager();

                            var pictureId = await galMan.UploadImage(data, userId);

                            if (pictureId == null)
                            {
                                throw new Exception();
                            }

                            var tempHotel = unitOfWork.Hotels.SetHotelImageADMIN(userId, finHotel.Id, pictureId ?? Guid.NewGuid());

                            unitOfWork.Complete();
                            finHotel = tempHotel;
                        }
                    }
                    catch (Exception)
                    {
                        //Maybe try to delete image.
                    }

                    return JsonFactory.CreateJsonMessage(OutgoingHotel.Parse(finHotel), HttpStatusCode.OK, this.Request);
                }
            }, this.Request));
        }