Exemplo n.º 1
0
    public bool Update(int userId, string firstName = "", string lastName = "", string status = "", int themeId = 0, Location home = null, Location work = null)
    {
        if (User.Where(s => s.Id == userId).Count() > 0)
        {
            User userUpdated = User.Where(w => w.Id == userId).First();

            userUpdated.FirstName = !string.IsNullOrEmpty(firstName) ? firstName : userUpdated.FirstName;
            userUpdated.LastName  = !string.IsNullOrEmpty(lastName) ? lastName : userUpdated.LastName;
            userUpdated.Status    = !string.IsNullOrEmpty(status) ? status : userUpdated.Status;
            userUpdated.ThemeId   = (themeId != 0 && UserTheme.Where(w => w.Id == themeId).Count() > 0) ? themeId : userUpdated.ThemeId;

            SaveChanges();

            if (home != null)
            {
                UserLocation homeUpdated = UserLocation.Where(w => w.Id == userUpdated.HomeId).First();

                homeUpdated.Latitude  = home.Latitude;
                homeUpdated.Longitude = home.Longitude;

                SaveChanges();
            }
            if (work != null)
            {
                if (userUpdated.WorkId != 0)
                {
                    UserLocation workUpdated = UserLocation.Where(w => w.Id == userUpdated.WorkId).First();

                    workUpdated.Latitude  = home.Latitude;
                    workUpdated.Longitude = home.Longitude;

                    SaveChanges();
                }
                else
                {
                    UserLocation workLoc = new UserLocation
                    {
                        IsHome    = true,
                        IsWork    = false,
                        Longitude = home.Longitude,
                        Latitude  = home.Latitude
                    };

                    workLoc = UserLocation.Add(workLoc);

                    SaveChanges();

                    userUpdated.WorkId = workLoc.Id;

                    SaveChanges();
                }
            }

            return(true);
        }


        return(true);
    }
Exemplo n.º 2
0
    public bool Register(string email, string password, string firstName, string lastName, string status, int themeId, Location home, Location work = null)
    {
        if (User.Where(s => s.Email == email).Count() > 0)
        {
            return(false);
        }
        else
        {
            UserLocation homeLoc = new UserLocation
            {
                IsHome    = true,
                IsWork    = false,
                Longitude = home.Longitude,
                Latitude  = home.Latitude
            };

            homeLoc = UserLocation.Add(homeLoc);

            SaveChanges();

            UserLocation workLoc = null;

            if (work != null)
            {
                workLoc = new UserLocation
                {
                    IsHome    = false,
                    IsWork    = true,
                    Longitude = work.Longitude,
                    Latitude  = work.Latitude
                };

                workLoc = UserLocation.Add(workLoc);

                SaveChanges();
            }

            byte[] salt = new byte[128 / 8];
            using (var rng = RandomNumberGenerator.Create())
            {
                rng.GetBytes(salt);
            }
            string base64Salt = Convert.ToBase64String(salt);

            string hashedPassword = Convert.ToBase64String(KeyDerivation.Pbkdf2(
                                                               password: password,
                                                               salt: salt,
                                                               prf: KeyDerivationPrf.HMACSHA256,
                                                               iterationCount: 10000,
                                                               numBytesRequested: 256 / 8));

            salt = new byte[128 / 8];
            using (var rng = RandomNumberGenerator.Create())
            {
                rng.GetBytes(salt);
            }
            string image = Convert.ToBase64String(salt);

            SetDefaultUserPhoto(image + ".png");

            var newUser = new User
            {
                FirstName = firstName,
                LastName  = lastName,
                Email     = email,
                Password  = hashedPassword,
                Salt      = base64Salt,
                ImageURL  = image + ".png",
                Status    = !string.IsNullOrEmpty(status) ? status : "Online",
                HomeId    = homeLoc.Id,
                WorkId    = workLoc != null ? workLoc.Id : 0,
                ThemeId   = (themeId != 0 && UserTheme.Where(w => w.Id == themeId).Count() > 0) ? themeId : 1
            };

            User.Add(newUser);

            SaveChanges();

            return(true);
        }
    }