Esempio n. 1
0
        public ActionResult Create(UserProfile userProfile)
        {
            byte[] salt     = PasswordEncryption.GenerateSalt();
            var    password = Encoding.UTF8.GetBytes(userProfile.Password);

            var hashedPassword = PasswordEncryption.HashPasswordWithSalt(password, salt);

            userProfile.Password = Convert.ToBase64String(hashedPassword);


            if (userProfile.Image != null)
            {
                string fileName        = Path.GetFileNameWithoutExtension(userProfile.ImageFile.FileName);
                string extension       = Path.GetExtension(userProfile.ImageFile.FileName);
                string imageFolderPath = "~/Images/";

                fileName          = fileName + DateTime.Now.ToString("yymmssfff") + extension;
                userProfile.Image = imageFolderPath + fileName;
                fileName          = Path.Combine(Server.MapPath(imageFolderPath), fileName);

                userProfile.ImageFile.SaveAs(fileName);
            }


            if (ModelState.IsValid)
            {
                db.UserProfile.Add(userProfile);
                db.SaveChanges();
                ModelState.Clear();

                return(RedirectToAction("Index"));
            }

            return(View(userProfile));
        }
Esempio n. 2
0
        public IHttpActionResult PutUserProfile(int id, UserProfile userProfile)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != userProfile.Id)
            {
                return(BadRequest());
            }

            db.Entry(userProfile).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserProfileExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IActionResult SetThumbnail([FromBody] TransferFile?transferFile)
        {
            try
            {
                using UserProfileContext userProfileContext = CreateUserProfileContext(out DbSet <RegisteredUser> registeredUsers, out _, out _);
                if (!IsTokenValid(registeredUsers, out RegisteredUser? loginUser))
                {
                    return(Unauthorized());
                }
                if (transferFile == null)
                {
                    return(BadRequest());
                }

                // 設定
                using MemoryStream memoryStream = new MemoryStream(transferFile.Content);
                loginUser.Bitmap       = ServerCommon.CreateThumbnail(memoryStream, transferFile.Mime, YbdConstants.USER_THUMBNAIL_WIDTH_MAX, YbdConstants.USER_THUMBNAIL_HEIGHT_MAX, true);
                loginUser.Mime         = transferFile.Mime;
                loginUser.LastModified = YbdCommon.UtcNowModifiedJulianDate();
                userProfileContext.SaveChanges();

                return(Ok());
            }
            catch (Exception excep)
            {
                Debug.WriteLine("プロフィール画像設定サーバーエラー:\n" + excep.Message);
                Debug.WriteLine(" スタックトレース:\n" + excep.StackTrace);
                return(InternalServerError());
            }
        }
        public IActionResult DeleteHistoriesAll()
        {
            try
            {
                using UserProfileContext userProfileContext = CreateUserProfileContext(out DbSet <RegisteredUser> registeredUsers, out _, out DbSet <HistorySong> historySongs);
                if (!IsTokenValid(registeredUsers, out RegisteredUser? loginUser))
                {
                    return(Unauthorized());
                }

                IQueryable <HistorySong> histories = historySongs.Where(x => x.UserId == loginUser.Id);
                if (!histories.Any())
                {
                    return(NotAcceptable());
                }

                // マイ履歴を削除
                historySongs.RemoveRange(histories);
                userProfileContext.SaveChanges();
                return(Ok());
            }
            catch (Exception excep)
            {
                Debug.WriteLine("マイ履歴すべて削除サーバーエラー:\n" + excep.Message);
                Debug.WriteLine(" スタックトレース:\n" + excep.StackTrace);
                return(InternalServerError());
            }
        }
Esempio n. 5
0
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = new UserProfileContext(
                       serviceProvider.GetRequiredService <
                           DbContextOptions <UserProfileContext> >()))
            {
                // Look for any movies.
                if (context.UserDetails.Any())
                {
                    return;   // DB has been seeded
                }

                context.UserDetails.AddRange(
                    new UserDetails
                {
                    userName  = "******",
                    alias     = "tomAlias",
                    website   = "tom.ca",
                    socialUrl = "friends.com/tom",
                    email     = "*****@*****.**",
                    dob       = DateTime.Parse("1989-2-12"),
                },

                    new UserDetails
                {
                    userName  = "******",
                    alias     = "jerryAlias",
                    website   = "jerry.ca",
                    socialUrl = "friends.com/jerry",
                    email     = "*****@*****.**",
                    dob       = DateTime.Parse("1989-2-24"),
                },

                    new UserDetails
                {
                    userName  = "******",
                    alias     = "pickachuAlias",
                    website   = "pickachu.ca",
                    socialUrl = "friends.com/pickachu",
                    email     = "*****@*****.**",
                    dob       = DateTime.Parse("1989-3-12"),
                },

                    new UserDetails
                {
                    userName  = "******",
                    alias     = "tweetyAlias",
                    website   = "tweety.ca",
                    socialUrl = "friends.com/tweety",
                    email     = "*****@*****.**",
                    dob       = DateTime.Parse("1989-4-12"),
                }
                    );
                context.SaveChanges();
            }
        }
        public IActionResult AddUser([FromBody] LoginInfo registerInfo)
        {
            try
            {
                if (!registerInfo.IsValid())
                {
                    return(BadRequest());
                }

                using UserProfileContext userProfileContext = CreateUserProfileContext(out DbSet <RegisteredUser> registeredUsers, out _, out _);
                RegisteredUser newUser = new();
                newUser.Name         = registerInfo.Name;
                newUser.Password     = registerInfo.Password;
                newUser.LastModified = newUser.LastLogin = YbdCommon.UtcNowModifiedJulianDate();

                if (!IsAdminRegistered(registeredUsers))
                {
                    // 管理者未登録の場合は管理者登録でなければならない
                    if (newUser.Name != YbdConstants.ADMIN_NAME)
                    {
                        return(BadRequest());
                    }
                    newUser.IsAdmin = true;
                }

                // 同じ名前のユーザーが既に存在している場合は登録できない
                if (registeredUsers.FirstOrDefault(x => x.Name == newUser.Name) != null)
                {
                    return(Conflict());
                }

                // 登録
                HashPassword(newUser);
                registeredUsers.Add(newUser);
                userProfileContext.SaveChanges();

                String idAndToken = GenerateIdAndTokenString(newUser.Id);
                Debug.WriteLine("AddUser() " + idAndToken);

                // 登録と同時にログインできるように ID とログイン用トークンを返す
                return(Ok(idAndToken));
            }
            catch (Exception excep)
            {
                Debug.WriteLine("ユーザー登録サーバーエラー:\n" + excep.Message);
                Debug.WriteLine(" スタックトレース:\n" + excep.StackTrace);
                return(InternalServerError());
            }
        }
        public IActionResult DeleteUser(String?id)
        {
            try
            {
                using UserProfileContext userProfileContext = CreateUserProfileContext(out DbSet <RegisteredUser> registeredUsers, out DbSet <StockSong> stockSongs, out DbSet <HistorySong> historySongs);
                if (!IsTokenValid(registeredUsers, out RegisteredUser? loginUser) || !loginUser.IsAdmin)
                {
                    return(Unauthorized());
                }
                if (String.IsNullOrEmpty(id))
                {
                    return(BadRequest());
                }

                RegisteredUser?deleteUser = registeredUsers.SingleOrDefault(x => x.Id == id);
                if (deleteUser == null)
                {
                    return(NotAcceptable());
                }
                if (deleteUser.IsAdmin)
                {
                    // 管理者は削除できない
                    return(NotAcceptable());
                }

                // 後で歌う予定リストを削除
                stockSongs.RemoveRange(stockSongs.Where(x => x.UserId == deleteUser.Id));

                // マイ履歴を削除
                historySongs.RemoveRange(historySongs.Where(x => x.UserId == deleteUser.Id));

                // 本体を削除
                registeredUsers.Remove(deleteUser);

#if DEBUG
                Thread.Sleep(1000);
#endif
                userProfileContext.SaveChanges();
                return(Ok());
            }
            catch (Exception excep)
            {
                Debug.WriteLine("ユーザー削除サーバーエラー:\n" + excep.Message);
                Debug.WriteLine(" スタックトレース:\n" + excep.StackTrace);
                return(InternalServerError());
            }
        }
        public IActionResult Login([FromBody] LoginInfo loginInfo)
        {
            try
            {
                if (!loginInfo.IsValid())
                {
                    return(BadRequest());
                }

#if DEBUG
                Thread.Sleep(1000);
#endif

                // ユーザーを検索
                using UserProfileContext userProfileContext = CreateUserProfileContext(out DbSet <RegisteredUser> registeredUsers, out _, out _);
                RegisteredUser?loginUser = registeredUsers.SingleOrDefault(x => x.Name == loginInfo.Name);
                if (loginUser == null)
                {
                    return(NotAcceptable());
                }

                // パスワードハッシュの一致を確認
                if (loginUser.Password != HashPassword(loginInfo.Password, loginUser.Salt))
                {
                    return(NotAcceptable());
                }

                String idAndToken = GenerateIdAndTokenString(loginUser.Id);
                Debug.WriteLine("Login() " + idAndToken);

                loginUser.LastLogin = YbdCommon.UtcNowModifiedJulianDate();
                userProfileContext.SaveChanges();

                // ID とログイン用トークンを返す
                return(Ok(idAndToken));
            }
            catch (Exception excep)
            {
                Debug.WriteLine("ログインサーバーエラー:\n" + excep.Message);
                Debug.WriteLine(" スタックトレース:\n" + excep.StackTrace);
                return(InternalServerError());
            }
        }
        public IActionResult SetPassword([FromBody] String?[] passwords)
        {
            try
            {
                using UserProfileContext userProfileContext = CreateUserProfileContext(out DbSet <RegisteredUser> registeredUsers, out _, out _);
                if (!IsTokenValid(registeredUsers, out RegisteredUser? loginUser))
                {
                    return(Unauthorized());
                }
                if (passwords.Length < 2)
                {
                    return(BadRequest());
                }
                String?currentPassword = passwords[0];
                String?newPassword     = passwords[1];
                if (String.IsNullOrEmpty(currentPassword) || String.IsNullOrEmpty(newPassword))
                {
                    return(BadRequest());
                }

                // 現在のパスワードハッシュの一致を確認
                if (loginUser.Password != HashPassword(currentPassword, loginUser.Salt))
                {
                    return(NotAcceptable());
                }

                // 設定
                loginUser.Password     = newPassword;
                loginUser.LastModified = YbdCommon.UtcNowModifiedJulianDate();
                HashPassword(loginUser);
                userProfileContext.SaveChanges();

                return(Ok());
            }
            catch (Exception excep)
            {
                Debug.WriteLine("パスワード設定サーバーエラー:\n" + excep.Message);
                Debug.WriteLine(" スタックトレース:\n" + excep.StackTrace);
                return(InternalServerError());
            }
        }
        public IActionResult SetName([FromBody] String?newName)
        {
            try
            {
                using UserProfileContext userProfileContext = CreateUserProfileContext(out DbSet <RegisteredUser> registeredUsers, out _, out _);
                if (!IsTokenValid(registeredUsers, out RegisteredUser? loginUser))
                {
                    return(Unauthorized());
                }
                if (String.IsNullOrEmpty(newName))
                {
                    return(BadRequest());
                }

                // 管理者の名前は変更できない
                if (loginUser.IsAdmin)
                {
                    return(BadRequest());
                }

                // 同じ名前のユーザーが既に存在している場合は登録できない
                if (registeredUsers.FirstOrDefault(x => x.Name == newName) != null)
                {
                    return(Conflict());
                }

                // 設定
                loginUser.Name         = newName;
                loginUser.LastModified = YbdCommon.UtcNowModifiedJulianDate();
                userProfileContext.SaveChanges();

                return(Ok());
            }
            catch (Exception excep)
            {
                Debug.WriteLine("名前設定サーバーエラー:\n" + excep.Message);
                Debug.WriteLine(" スタックトレース:\n" + excep.StackTrace);
                return(InternalServerError());
            }
        }
        public IActionResult DeleteStock(String?stockSongId)
        {
            try
            {
                using UserProfileContext userProfileContext = CreateUserProfileContext(out DbSet <RegisteredUser> registeredUsers, out DbSet <StockSong> stockSongs, out _);
                if (!IsTokenValid(registeredUsers, out RegisteredUser? loginUser))
                {
                    return(Unauthorized());
                }
                if (String.IsNullOrEmpty(stockSongId))
                {
                    return(BadRequest());
                }
                if (!Int32.TryParse(stockSongId, out Int32 stockSongIdNum))
                {
                    return(BadRequest());
                }
                StockSong?stockSong = stockSongs.SingleOrDefault(x => x.StockSongId == stockSongIdNum);
                if (stockSong == null)
                {
                    return(NotAcceptable());
                }
                if (stockSong.UserId != loginUser.Id)
                {
                    return(Unauthorized());
                }

                // 後で歌う予定リストから削除
                stockSongs.Remove(stockSong);
                userProfileContext.SaveChanges();
                return(Ok());
            }
            catch (Exception excep)
            {
                Debug.WriteLine("後で歌う予定リスト削除サーバーエラー:\n" + excep.Message);
                Debug.WriteLine(" スタックトレース:\n" + excep.StackTrace);
                return(InternalServerError());
            }
        }
        public IActionResult AddStock([FromBody] AvailableSong availableSong)
        {
            try
            {
                using UserProfileContext userProfileContext = CreateUserProfileContext(out DbSet <RegisteredUser> registeredUsers, out DbSet <StockSong> stockSongs, out _);
                if (!IsTokenValid(registeredUsers, out RegisteredUser? loginUser))
                {
                    return(Unauthorized());
                }

                StockSong?stockSong = stockSongs.SingleOrDefault(x => x.UserId == loginUser.Id && x.AvailableSongId == availableSong.Id);
                if (stockSong == null)
                {
                    // 新規追加
                    stockSong = new();
                    YbdCommon.CopySongProperty(availableSong, stockSong);
                    stockSong.AvailableSongId = availableSong.Id;
                    stockSong.UserId          = loginUser.Id;
                    stockSong.RequestTime     = YbdCommon.UtcNowModifiedJulianDate();
                    stockSongs.Add(stockSong);
                }
                else
                {
                    // 登録日時更新
                    stockSong.RequestTime = YbdCommon.UtcNowModifiedJulianDate();
                }
                userProfileContext.SaveChanges();

                return(Ok());
            }
            catch (Exception excep)
            {
                Debug.WriteLine("後で歌う予定追加サーバーエラー:\n" + excep.Message);
                Debug.WriteLine(" スタックトレース:\n" + excep.StackTrace);
                return(InternalServerError());
            }
        }
Esempio n. 13
0
 public void Add(User item)
 {
     _context.Users.Add(item);
     _context.SaveChanges();
 }
Esempio n. 14
0
        public IActionResult AddRequestSong([FromBody] RequestSong requestSong)
        {
            try
            {
                if (!requestSong.IsValid())
                {
                    return(BadRequest());
                }

                // 予約者のユーザー ID が指定されている場合はその正当性を確認(なりすまし予約防止)
                using UserProfileContext userProfileContext = CreateUserProfileContext(out DbSet <RegisteredUser> registeredUsers, out DbSet <StockSong> stockSongs, out DbSet <HistorySong> historySongs);
                if (!String.IsNullOrEmpty(requestSong.UserId))
                {
                    if (!IsTokenValid(registeredUsers, out RegisteredUser? loginUser) || requestSong.UserId != loginUser.Id)
                    {
                        return(Unauthorized());
                    }
                }

                // 追加する曲の位置は最後
                using RequestSongContext requestSongContext = CreateRequestSongContext(out DbSet <RequestSong> requestSongs);
                Int32 sort;
                if (requestSongs.Any())
                {
                    sort = requestSongs.Max(x => x.Sort) + 1;
                }
                else
                {
                    sort = 1;
                }
                requestSong.Sort = sort;

                // 予約追加
                requestSongs.Add(requestSong);
                requestSongContext.SaveChanges();

                if (!String.IsNullOrEmpty(requestSong.UserId))
                {
                    // 予約者のユーザー ID が指定されている場合は履歴追加
                    HistorySong historySong = new();
                    YbdCommon.CopyHistorySongProperty(requestSong, historySong);
                    historySongs.Add(historySong);

                    // 後で歌う予定リストに追加されている場合はリストから削除
                    StockSong?stockSong = SearchStockSongByRequestSong(stockSongs, requestSong);
                    if (stockSong != null)
                    {
                        stockSongs.Remove(stockSong);
                    }

                    userProfileContext.SaveChanges();
                }

                SendSse(YbdConstants.SSE_DATA_REQUEST_CHANGED);
                return(Ok());
            }
            catch (Exception excep)
            {
                Debug.WriteLine("予約追加サーバーエラー:\n" + excep.Message);
                Debug.WriteLine(" スタックトレース:\n" + excep.StackTrace);
                return(InternalServerError());
            }
        }