public LoginResponseDto Login(LoginDto dto)
        {
            var result = new LoginResponseDto();

            dto.Password = FunctionCommon.GetMd5(FunctionCommon.GetSimpleMd5(dto.Password));

            var accountFromDb = db.Accounts.FirstOrDefault(x => x.UserName == dto.UserName && x.Password == dto.Password && !x.DelFlag);

            if (accountFromDb == null)
            {
                return null;
            }

            var isStudent = db.AccountGroups.FirstOrDefault(x => !x.DelFlag && x.Name.ToLower().Equals("Sinh viên")).Id == accountFromDb.AccountGroupId;

            var accessToken = JwtAuthenticationExtensions.CreateToken(accountFromDb, isStudent);

            result.AccessToken = accessToken;

            result.FirstName = accountFromDb.UserInfo.FirstName;

            result.LastName = accountFromDb.UserInfo.LastName;

            result.Avatar = accountFromDb.Avatar;

            result.UserName = accountFromDb.UserName;

            result.IsStudent = isStudent;

            return result;
        }
        public bool UpdatePass(UpdatePassword updatePassword, string token)
        {
            int Id = JwtAuthenticationExtensions.ExtractTokenInformation(token).UserId;

            if (string.Compare(FunctionCommon.GetMd5(FunctionCommon.GetSimpleMd5(updatePassword.OldPassword)), context.Accounts.FirstOrDefault(x => x.UserInfoId == Id && !x.DelFlag).Password) != 0)
            {
                return(false);
            }
            else
            {
                var TaiKhoan = context.Accounts.FirstOrDefault(x => x.UserInfoId == Id && !x.DelFlag);
                TaiKhoan.Password = FunctionCommon.GetMd5(FunctionCommon.GetSimpleMd5(updatePassword.NewPassword));
                context.SaveChanges();
                return(true);
            }
        }
 public bool ForgetPass(ForgetPassword input)
 {
     var taikhoan = db.Accounts.FirstOrDefault(x => string.Compare(x.UserName, input.Username) == 0 && string.Compare(x.UserInfo.Email, input.Email) == 0 && !x.DelFlag);
     if (taikhoan == null)
     {
         return false;
     }
     else
     {
         string newPass = FunctionCommon.AutoPassword();
         SendMail.Send(taikhoan.UserInfo.Email, newPass, "[DUTAdmissionSystem] Forget Password");
         taikhoan.Password = FunctionCommon.GetMd5(FunctionCommon.GetSimpleMd5(newPass));
         db.SaveChanges();
         return true;
     }
 }
Пример #4
0
        public int ChangePassword(PasswordDto password, int id)
        {
            var account = db.Accounts.FirstOrDefault(x => !x.DelFlag && x.UserId == id);

            if (account == null)
            {
                return(-1);                 // cannot found this account
            }
            if (!account.Password.Equals(FunctionCommon.GetMd5(FunctionCommon.GetSimpleMd5(password.OldPassword))))
            {
                return(0);
            }

            account.Password = FunctionCommon.GetMd5(FunctionCommon.GetSimpleMd5(password.NewPassword));

            db.SaveChanges();
            return(1);
        }
Пример #5
0
        public bool AddParent(ParentDto parent)
        {
            try
            {
                var user = new EM.Database.Schema.User
                {
                    FirstName            = parent.FirstName,
                    LastName             = parent.LastName,
                    Gender               = parent.Gender,
                    Birthday             = parent.Birthday,
                    Address              = parent.Address,
                    PhoneNumber          = parent.PhoneNumber,
                    IdentificationNumber = parent.IdentificationNumber,
                    Avatar               = parent.Avatar
                };

                db.Users.Add(user);
                db.SaveChanges();
                var account = new EM.Database.Schema.Account
                {
                    UserName = parent.Username,
                    Password = FunctionCommon.GetMd5(FunctionCommon.GetSimpleMd5(parent.Password)),
                    UserId   = user.Id,
                    GroupId  = db.Groups.FirstOrDefault(x => !x.DelFlag && x.Name.Equals("Parent")).Id
                };

                db.Accounts.Add(account);
                db.SaveChanges();

                var parentFromDb = new EM.Database.Schema.Parent
                {
                    UserId = user.Id
                };
                db.Parents.Add(parentFromDb);
                db.SaveChanges();

                return(true);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public bool AddUser(UserInfoDto userInfo)
        {
            try
            {
                var user = new EM.Database.Schema.User
                {
                    FirstName            = userInfo.FirstName,
                    LastName             = userInfo.LastName,
                    Gender               = userInfo.Gender,
                    Birthday             = userInfo.Birthday,
                    Address              = userInfo.Address,
                    PhoneNumber          = userInfo.PhoneNumber,
                    IdentificationNumber = userInfo.IdentificationNumber,
                    Avatar               = userInfo.Avatar
                };

                db.Users.Add(user);
                db.SaveChanges();
                var account = new EM.Database.Schema.Account
                {
                    UserName = userInfo.Username,
                    Password = FunctionCommon.GetMd5(FunctionCommon.GetSimpleMd5(userInfo.Password)),
                    UserId   = user.Id,
                    GroupId  = userInfo.IsAdmin ? db.Groups.FirstOrDefault(x => !x.DelFlag && x.Name.Equals("Admin")).Id :
                               db.Groups.FirstOrDefault(x => !x.DelFlag && x.Name.Equals("Mod")).Id
                };

                db.Accounts.Add(account);
                db.SaveChanges();
                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
        public void getExcelFile()
        {
            DataContext context = new DataContext();

            //Create COM Objects. Create a COM object for everything that is referenced
            Excel.Application xlApp       = new Excel.Application();
            Excel.Workbook    xlWorkbook  = xlApp.Workbooks.Open(@"C:\Users\Aiden\Desktop\data.xlsx");
            Excel._Worksheet  xlWorksheet = xlWorkbook.Sheets[1];
            Excel.Range       xlRange     = xlWorksheet.UsedRange;

            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;

            //iterate over the rows and columns and print to the console as it appears in the file
            //excel is not zero based!!
            for (int i = 1; i <= 50; i++)
            {
                var user = new UserInfo()
                {
                    FirstName = xlRange.Cells[i, 4].Value2.ToString(),
                    LastName  = xlRange.Cells[i, 3].Value2.ToString(),
                    Avatar    = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT_S9DUg_S9CHf-DxgcNbxYzZmibzud95wxTQslnreREOxA1ch1",
                    BirthInfo = new BirthInfo()
                    {
                        Sex          = true,
                        DateOfBirth  = DateTime.ParseExact(xlRange.Cells[i, 5].Value2.ToString(), "dd/MM/yyyy", null),
                        PlaceOfBirth = xlRange.Cells[i, 10].Value2.ToString()
                    },
                    ContactInfo = new ContactInfo()
                    {
                        Address     = xlRange.Cells[i, 10].Value2.ToString(),
                        Email       = xlRange.Cells[i, 8].Value2.ToString(),
                        PhoneNumber = xlRange.Cells[i, 7].Value2.ToString()
                    },
                    IdentityInfo = new IdentityInfo()
                    {
                        IdentityNumber = xlRange.Cells[i, 11].Value2.ToString(),
                        DateOfIssue    = DateTime.Now,
                        PlaceOfIssue   = "Việt Nam"
                    }
                };

                var student = new Student()
                {
                    UserInfo             = user,
                    IdentificationNumber = xlRange.Cells[i, 2].Value2.ToString(),
                    HightSchoolName      = "THPT",
                    ElectionTypeId       = 1,
                    YouthGroupInfo       = null,
                    CircumstanceTypeId   = 1,
                    EnrollmentAreaId     = Convert.ToInt32(xlRange.Cells[i, 9].Value2.ToString()),
                    ClassId      = Convert.ToInt32(xlRange.Cells[i, 1].Value2.ToString()),
                    PersonalInfo = new PersonalInfo()
                    {
                        EthnicId           = 1,
                        NationalityId      = 1,
                        ReligionId         = 1,
                        PermanentResidence = xlRange.Cells[i, 10].Value2.ToString()
                    },
                };

                var account = new Account()
                {
                    UserInfo       = user,
                    AccountGroupId = 2,
                    Token          = "",
                    UserName       = xlRange.Cells[i, 2].Value2.ToString(),
                    Password       = FunctionCommon.GetMd5(FunctionCommon.GetSimpleMd5(xlRange.Cells[i, 2].Value2.ToString()))
                };

                context.Accounts.Add(account);
                context.Students.Add(student);
            }

            context.SaveChanges();
            //cleanup
            GC.Collect();
            GC.WaitForPendingFinalizers();

            //rule of thumb for releasing com objects:
            //  never use two dots, all COM objects must be referenced and released individually
            //  ex: [somthing].[something].[something] is bad

            //release com objects to fully kill excel process from running in the background
            Marshal.ReleaseComObject(xlRange);
            Marshal.ReleaseComObject(xlWorksheet);

            //close and release
            xlWorkbook.Close();
            Marshal.ReleaseComObject(xlWorkbook);

            //quit and release
            xlApp.Quit();
            Marshal.ReleaseComObject(xlApp);
        }
        public StudentDto UpdateStudent(StudentDto studentInput)
        {
            if (studentInput.Id == 0)
            {
                var user = new UserInfo()
                {
                    FirstName = studentInput.FirstName,
                    LastName  = studentInput.LastName,
                    Avatar    = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT_S9DUg_S9CHf-DxgcNbxYzZmibzud95wxTQslnreREOxA1ch1",
                    BirthInfo = new BirthInfo()
                    {
                        Sex          = true,
                        DateOfBirth  = studentInput.DateOfBirth,
                        PlaceOfBirth = "Việt Nam"
                    },
                    ContactInfo = new ContactInfo()
                    {
                        Address     = studentInput.Address,
                        Email       = studentInput.Email,
                        PhoneNumber = studentInput.PhoneNumber
                    },
                    IdentityInfo = new IdentityInfo()
                    {
                        IdentityNumber = studentInput.IdentityNumber,
                        DateOfIssue    = DateTime.Now,
                        PlaceOfIssue   = "Việt Nam"
                    }
                };

                var student = new Student()
                {
                    UserInfo             = user,
                    IdentificationNumber = studentInput.IdentificationNumber,
                    HightSchoolName      = "THPT",
                    ElectionTypeId       = 1,
                    YouthGroupInfo       = null,
                    CircumstanceTypeId   = 1,
                    EnrollmentAreaId     = 1,
                    ClassId      = studentInput.ClassId,
                    PersonalInfo = new PersonalInfo()
                    {
                        EthnicId           = 1,
                        NationalityId      = 1,
                        ReligionId         = 1,
                        PermanentResidence = "Việt Nam"
                    },
                };

                var account = new Account()
                {
                    UserInfo       = user,
                    AccountGroupId = 2,
                    Token          = "",
                    UserName       = studentInput.IdentificationNumber,
                    Password       = FunctionCommon.GetMd5(FunctionCommon.GetSimpleMd5(studentInput.IdentificationNumber))
                };

                db.Accounts.Add(account);
                db.Students.Add(student);
            }
            else
            {
                var student = db.Students.FirstOrDefault(x => x.Id == studentInput.Id && !x.DelFlag);
                if (student != null)
                {
                    student.UserInfo.FirstName                   = studentInput.FirstName;
                    student.UserInfo.LastName                    = studentInput.LastName;
                    student.UserInfo.BirthInfo.DateOfBirth       = studentInput.DateOfBirth;
                    student.UserInfo.IdentityInfo.IdentityNumber = studentInput.IdentityNumber;
                    student.UserInfo.ContactInfo.PhoneNumber     = studentInput.PhoneNumber;
                    student.UserInfo.ContactInfo.Email           = studentInput.Email;
                    student.UserInfo.ContactInfo.Address         = studentInput.Address;
                    student.ClassId = studentInput.ClassId;
                }
            }
            db.SaveChanges();
            studentInput.ClassName = db.Classes.FirstOrDefault(x => x.Id == studentInput.ClassId && !x.DelFlag).Name;
            return(studentInput);
        }