Exemplo n.º 1
0
        public async Task <IActionResult> AddUser(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "users/add")] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("request - /api/users/add");

            try
            {
                var         content = await new StreamReader(req.Body).ReadToEndAsync();
                PostNewUser input   = JsonConvert.DeserializeObject <PostNewUser>(content);

                Location location = dbContext.Locations.FirstOrDefault(l => l.ID == input.LocationID);
                User     newUser  = new User()
                {
                    FirstName = input.FirstName, LastName = input.LastName, Phone = input.Phone, Location = location
                };
                dbContext.Users.Add(newUser);
                dbContext.SaveChanges();
                return(new JsonResult(new { ID = newUser.ID, FirstName = newUser.FirstName, LastName = newUser.LastName, Phone = newUser.Phone, Location = newUser.Location.Name }));
            }
            catch (Exception ex)
            {
                log.LogError("ERROR: /api/users/add - " + ex.Message);
                return(new OkObjectResult(ex.Message));
            }
        }
Exemplo n.º 2
0
        public void CreateGroup(Group group)
        {
            using (var db = new UserManagementDBContext())
            {
                group.Deleted = false;

                db.Groups.Add(group);
                db.SaveChanges();
            }
        }
Exemplo n.º 3
0
        public void DeactivateUser(Guid id)
        {
            using (var db = new UserManagementDBContext())
            {
                var user = db.Users.Single(u => u.Id == id);
                user.IsApproved = false;
                user.ApprovedBy = null;

                db.SaveChanges();
            }
        }
Exemplo n.º 4
0
        public void ActivateUser(Guid id)
        {
            using (var db = new UserManagementDBContext())
            {
                var user = db.Users.Single(u => u.Id == id);
                user.IsApproved = true;
                user.ApprovedBy = GetCurrentUser().Id;

                db.SaveChanges();
            }
        }
Exemplo n.º 5
0
        public void RemoveUserFromGroup(Group group, User user)
        {
            using (var db = new UserManagementDBContext())
            {
                db.Users.Attach(user);
                db.Groups.Attach(group);

                db.Users.Remove(user);

                db.SaveChanges();
            }
        }
Exemplo n.º 6
0
        public void AddUserToGroup(Group group, User user)
        {
            using (var db = new UserManagementDBContext())
            {
                db.Users.Attach(user);
                db.Groups.Attach(group);

                group.Users.Add(user);

                db.SaveChanges();
            }
        }
Exemplo n.º 7
0
        public void EditGroup(int id, Group group)
        {
            using (var db = new UserManagementDBContext())
            {
                var oldGroup = db.Groups.Single(g => g.Id == id && !g.Deleted);

                oldGroup.Name = group.Name;
                db.SaveChanges();
            }

            _LmsService.Inform(UserNotifications.GroupEdit, group);
        }
Exemplo n.º 8
0
        public void ChangePassword(ChangePasswordModel changePasswordModel)
        {
            var user = GetCurrentUser();

            using (var db = new UserManagementDBContext())
            {
                user.Password = EncryptPassword(changePasswordModel.NewPassword);

                db.SaveChanges();
            }

            SendEmail("admin@iudico", user.Email, "Iudico Notification", "Your passord has been changed.");
        }
Exemplo n.º 9
0
        public void DeleteGroup(int id)
        {
            using (var db = new UserManagementDBContext())
            {
                var group = db.Groups.Single(g => g.Id == id && !g.Deleted);

                group.Users.Clear();
                group.Deleted = true;

                db.SaveChanges();

                _LmsService.Inform(UserNotifications.GroupDelete, group);
            }
        }
Exemplo n.º 10
0
        public void DeleteUser(Func <User, bool> predicate)
        {
            using (var db = new UserManagementDBContext())
            {
                var user = db.Users.Where(u => !u.Deleted).Single(predicate);

                user.Deleted = true;
                user.Groups.Clear();

                db.SaveChanges();

                _LmsService.Inform(UserNotifications.UserDelete, user);
            }
        }
Exemplo n.º 11
0
        public void EditAccount(EditModel editModel)
        {
            var identity = HttpContext.Current.User.Identity;

            using (var db = new UserManagementDBContext())
            {
                var user = db.Users.Single(u => u.Username == identity.Name);

                user.Name   = editModel.Name;
                user.OpenId = editModel.OpenId ?? string.Empty;
                user.Email  = editModel.Email;

                db.SaveChanges();

                SendEmail("admin@iudico", user.Email, "Iudico Notification", "Your details have been changed.");
            }
        }
Exemplo n.º 12
0
        public void CreateUser(User user)
        {
            using (var db = new UserManagementDBContext())
            {
                user.Password     = EncryptPassword(user.Password);
                user.OpenId       = user.OpenId ?? string.Empty;
                user.Deleted      = false;
                user.IsApproved   = true;
                user.CreationDate = DateTime.Now;
                user.ApprovedBy   = GetCurrentUser().Id;

                db.Users.Add(user);
                db.SaveChanges();
            }

            _LmsService.Inform(UserNotifications.UserCreate, user);
        }
Exemplo n.º 13
0
        public void EditUser(Guid id, EditUserModel user)
        {
            using (var db = new UserManagementDBContext())
            {
                var oldUser = db.Users.Single(u => u.Id == id);

                oldUser.Name = user.Name;
                if (user.Password != null && user.Password != string.Empty)
                {
                    oldUser.Password = EncryptPassword(user.Password);
                }
                oldUser.Email  = user.Email;
                oldUser.OpenId = user.OpenId ?? string.Empty;
                oldUser.RoleId = user.RoleId;

                db.SaveChanges();

                _LmsService.Inform(UserNotifications.UserEdit, oldUser);
            }
        }
Exemplo n.º 14
0
        public void RegisterUser(RegisterModel registerModel)
        {
            using (var db = new UserManagementDBContext())
            {
                var user = new User
                {
                    Username     = registerModel.Username,
                    Password     = EncryptPassword(registerModel.Password),
                    OpenId       = registerModel.OpenId ?? string.Empty,
                    Email        = registerModel.Email,
                    Name         = registerModel.Name,
                    Role         = Role.Student,
                    IsApproved   = false,
                    Deleted      = false,
                    CreationDate = DateTime.Now,
                    ApprovedBy   = null
                };

                db.Users.Add(user);
                db.SaveChanges();
            }
        }
Exemplo n.º 15
0
 public void Commit()
 {
     Dbcontext.SaveChanges();
 }