Example #1
0
        //First User Creation
        public async Task <string> CreateUserAsync(RegisterModel reg)
        {
            var newUser = new Admin();

            var dept = new Department()
            {
                Name = reg.Department
            };

            var per = new Person()
            {
                firstName   = reg.firstName,
                LastName    = reg.LastName,
                Gender      = reg.Gender,
                DateOfBirth = reg.DateOfBirth,
            };

            newUser.Email   = reg.EmailAddress;
            newUser.Address = reg.Address;
            newUser.Phone   = reg.Phone;
            newUser.Status  = true;

            // Password Hash
            // var message = reg.Password;
            var message = reg.LastName.ToLower();
            var salt    = Salt.Create();
            var hash    = Hash.Create(message, salt);

            newUser.PasswordHash = hash;
            newUser.salt         = salt;

            newUser.Person     = per;
            newUser.Department = dept;

            newUser.SearchString = (newUser.Person.LastName + " " + newUser.Person.firstName + " " + newUser.Person.Gender + " " + newUser.Email + " " + newUser.Phone + " Administrator" + " " + "Active").ToUpper();

            await _context.Users.AddAsync(newUser);

            await _context.SaveChangesAsync();


            // retrieve id of newly created item
            var thisUser = _context.Admins.
                           Where(x => x.Email.CompareTo(reg.EmailAddress) == 0 &&
                                 x.PasswordHash.CompareTo(hash) == 0).FirstOrDefault();

            int thisUserID = thisUser.Id;

            var change = new Change()
            {
                Table            = "Users",
                ChangeType       = "Create",
                EntryId          = thisUserID,
                OnlineTimeStamp  = DateTime.Now,
                OfflineTimeStamp = DateTime.Now,
            };
            var changeDept = new Change()
            {
                Table            = "Departments",
                ChangeType       = "Create",
                EntryId          = thisUserID,
                OnlineTimeStamp  = DateTime.Now,
                OfflineTimeStamp = DateTime.Now
            };

            await _context.Changes.AddAsync(changeDept);

            await _context.SaveChangesAsync();

            await _context.Changes.AddAsync(change);

            await _context.SaveChangesAsync();

            return("done");
        }
Example #2
0
        public async Task CreateAdmin(RegisterModel reg, int userId)
        {
            var newUser = new Admin();


            var per = new Person()
            {
                firstName   = reg.firstName,
                LastName    = reg.LastName,
                Gender      = reg.Gender,
                DateOfBirth = reg.DateOfBirth,
            };

            newUser.Email   = reg.EmailAddress;
            newUser.Address = reg.Address;
            newUser.Phone   = reg.Phone;


            // Password Hash
            //var message = reg.Password;
            var message = reg.LastName.ToLower();
            var salt    = Salt.Create();
            var hash    = Hash.Create(message, salt);

            newUser.PasswordHash = hash;
            newUser.salt         = salt;
            newUser.Status       = true;


            newUser.Person = per;

            // var DeptCheck = await _context.Departments.Where(x => x.Id == reg.DepartmentId).FirstOrDefaultAsync();
            // string errMessage = "Department does not exist";
            // if (DeptCheck == null)
            // {
            //     throw new Exception(errMessage);

            // }
            newUser.DepartmentId = reg.DepartmentId.Value;//_context.Departments.SingleOrDefaultAsync(x => x.Name.ToUpper().CompareTo(reg.Department.ToUpper()) == 0).Id;


            //await _context.SaveChangesAsync();
            newUser.SearchString = (newUser.Person.LastName + " " + newUser.Person.firstName + " " + newUser.Person.Gender + " " + newUser.Email + " " + newUser.Phone + " " + newUser.Status + " Administrator" + " " + "Active").ToUpper();
            await _context.Admins.AddAsync(newUser);

            await _context.SaveChangesAsync();

            var thisUser = _context.Admins.
                           Where(x => x.Email.CompareTo(reg.EmailAddress) == 0 &&
                                 x.PasswordHash.CompareTo(hash) == 0).FirstOrDefault();

            int thisUserID = thisUser.Id;

            var change = new Change()
            {
                Table            = "Users",
                EntryId          = thisUserID,
                ChangeType       = "Create",
                OnlineTimeStamp  = DateTime.Now,
                OfflineTimeStamp = DateTime.Now,
                UserId           = userId,
            };
            await _context.Changes.AddAsync(change);;
            await _context.SaveChangesAsync();
        }