Ejemplo n.º 1
0
        public void SendPrivateMessage(string userId, string message)
        {
            using (UWCContext db = new UWCContext())
            {
                User senderUser = db
                                  .Users
                                  .Where(u => u.Name == HttpContext.Current.User.Identity.Name)
                                  .FirstOrDefault();

                User recieverUser = db
                                    .Users
                                    .Where(u => u.Id.ToString() == userId)
                                    .FirstOrDefault();

                PrivateRoom privateRoom = db
                                          .PrivateRooms
                                          .Where(pr => pr.UserIds.Contains(senderUser.Id) && pr.UserIds.Contains(Guid.Parse(userId)))
                                          .FirstOrDefault();

                if (privateRoom == null)
                {
                    privateRoom = ChatHub.CreatePrivateRoom(senderUser.Id, recieverUser.Id);
                    db.PrivateRooms.Add(privateRoom);
                }

                string encodedMessage = AesEncoder.EncryptToBytes(message, Encoding.Default.GetBytes(privateRoom.Key), Encoding.Default.GetBytes(privateRoom.IV)).ToString();
                privateRoom.MessagesListing.Add(new MessageInfo()
                {
                    Id           = Guid.NewGuid(),
                    FromUserId   = senderUser.Id,
                    FromUserName = senderUser.Name,
                    ToUserId     = recieverUser.Id,
                    ToUserName   = recieverUser.Name,
                    Message      = encodedMessage,
                    Time         = DateTime.UtcNow
                });
                db.Entry(privateRoom).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();

                Groups.Add(senderUser.Id.ToString(), privateRoom.Id.ToString());
                Groups.Add(recieverUser.Id.ToString(), privateRoom.Id.ToString());
                Clients.Group(privateRoom.Id.ToString(), senderUser.Id.ToString()).send(message);
            }
        }
Ejemplo n.º 2
0
        public ActionResult ShowProfile()
        {
            if (User.Identity.IsAuthenticated)
            {
                using (UWCContext db = new UWCContext())
                {
                    User user = db
                                .Users
                                .Where(u => u.Email == User.Identity.Name)
                                .FirstOrDefault();
                    ViewBag.User         = user;
                    ViewBag.UserRoleName = new AppRoleProvider().GetRolesForUser(User.Identity.Name)[0];
                }

                return(View());
            }
            else
            {
                return(RedirectToAction("Login", "Account"));
            }
        }
Ejemplo n.º 3
0
        public ActionResult SetAdminRecord(SetAdminRecordModel model)
        {
            if (ModelState.IsValid)
            {
                using (UWCContext db = new UWCContext())
                {
                    string roleName = model.RoleName == "Преподаватель"
                                    ? UserRoles.TEACHER_ROLE_NAME
                                    : UserRoles.STUDENT_ROLE_NAME;

                    AdminRecord record = db.AdminRecords.FirstOrDefault(r => r.RoleName == roleName);
                    Guid        salt   = Guid.NewGuid();
                    if (record != null)
                    {
                        record.Salt            = salt;
                        record.Password        = Rfc2898Encoder.Encode(model.AccessPassword, salt.ToString());
                        db.Entry(record).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    else
                    {
                        AdminRecord newRecord = new AdminRecord()
                        {
                            Salt     = salt,
                            RoleName = roleName,
                            Password = Rfc2898Encoder.Encode(model.AccessPassword, salt.ToString())
                        };


                        db.AdminRecords.Add(newRecord);
                        db.SaveChanges();
                    }
                    return(RedirectToAction("AdminRecordAddedMessage", "Administration"));
                }
            }
            ViewBag.AllowedRoles = new SelectList(new string[] { "Преподаватель", "Студент" });
            return(View(model));
        }
Ejemplo n.º 4
0
        public ActionResult Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                User user = null;
                using (UWCContext db = new UWCContext())
                {
                    user = db.Users.FirstOrDefault(u => u.Email == model.Email);
                }

                if (user != null && Rfc2898Encoder.Validate(model.Password, user.Password, user.Id.ToString()))
                {
                    FormsAuthentication.SetAuthCookie(model.Email, true);
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    ModelState.AddModelError("", "Неправильный логин или пароль");
                }
            }

            return(View(model));
        }
Ejemplo n.º 5
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                User user = null;
                using (UWCContext db = new UWCContext())
                {
                    user = db.Users.FirstOrDefault(u => u.Email == model.Email);
                }

                if (user == null)
                {
                    using (UWCContext db = new UWCContext())
                    {
                        string roleName = model.RoleName == "Преподаватель"
                                   ? UserRoles.TEACHER_ROLE_NAME
                                   : UserRoles.STUDENT_ROLE_NAME;

                        AdminRecord adminRecord = db.AdminRecords.FirstOrDefault(r => r.RoleName == roleName);
                        if (adminRecord != null)
                        {
                            Guid   uid             = Guid.NewGuid();
                            string userSalt        = uid.ToString();
                            string encodedPassword = Rfc2898Encoder.Encode(model.Password, userSalt);

                            if (Rfc2898Encoder.Validate(model.RoleAccessPassword, adminRecord.Password, adminRecord.Salt.ToString()))
                            {
                                User newUser = new User()
                                {
                                    Id         = uid,
                                    Surname    = model.Surname,
                                    Name       = model.Name,
                                    Patronymic = model.Patronymic,
                                    Age        = model.Age,
                                    Email      = model.Email,
                                    Password   = encodedPassword,
                                    RoleId     = model.RoleName == "Преподаватель"
                                             ? UserRoles.TEACHER_ROLE_ID
                                             : UserRoles.STUDENT_ROLE_ID
                                };

                                db.Users.Add(newUser);
                                db.SaveChanges();
                            }
                            else
                            {
                                ModelState.AddModelError("", "Неверный пользовательский или преподавательский пароль");
                            }

                            user = db.Users.Where(u => u.Email == model.Email && u.Password == encodedPassword).FirstOrDefault();
                        }
                        else
                        {
                            ModelState.AddModelError("", "Пароль для регистрации с ролью \"" + roleName + "\" еще не задан администратором, попробуйте позже.");
                        }
                    }
                    if (user != null)
                    {
                        FormsAuthentication.SetAuthCookie(model.Email, true);
                        return(RedirectToAction("Index", "Home"));
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Пользоваетль с таким адресом Email уже существует");
                }
            }

            ViewBag.AllowedRoles = new SelectList(new string[] { "Преподаватель", "Студент" });
            return(View(model));
        }