public async Task<IActionResult> GetUsersByRole(Guid? id, string role)
        {
            if (id == null)
            {
                return NotFound();
            }

            var mgroup = await _context.Groups
                .Include(m => m.GroupUsers)
                .FirstOrDefaultAsync(m => m.GroupId == id);
            if (mgroup == null)
            {
                return NotFound();
            }

            List<string> usersList = new List<string>();
            if (role == "ALL") usersList = UserHelper.GetAllUsers(Configuration["JWT:Admin-Token-Url"], Configuration["JWT:Admin-Url"], Configuration["JWT:Admin-Key"], Configuration["JWT:Admin-User"], Configuration["JWT:Admin-Password"]);
            else usersList = UserHelper.GetAllUsersByRole(role, Configuration["JWT:Admin-Url"], Configuration["JWT:ClientId"], Configuration["JWT:Admin-Token-Url"], Configuration["JWT:Admin-Key"], Configuration["JWT:Admin-User"], Configuration["JWT:Admin-Password"]);
            
            List<SelectedUserPoco> returnList = new List<SelectedUserPoco>();
            foreach (string userid in usersList)
            {
                DB.Models.Domain.User u = await _context.Users.FindAsync(userid);
                SelectedUserPoco sup = new SelectedUserPoco() { myUser = u, myGroup = mgroup };
                if (u != null) returnList.Add(sup);
            }

            string json = JsonConvert.SerializeObject(returnList);
            return Content(json, "application/json");
        }
        public async Task<IActionResult> GetProjectsForGroup(Guid? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var mgroup = await _context.Groups
                .Include(m => m.GroupProjects).ThenInclude(m => m.Geometries)
                .Include(m => m.GroupProjects).ThenInclude(m => m.Records)
                .Include(m => m.GroupUsers)
                .FirstOrDefaultAsync(m => m.GroupId == id);
            if (mgroup == null)
            {
                return NotFound();
            }

            DB.Models.Domain.User me = UserHelper.GetCurrentUser(User, _context);

            List<Project> projects;
            if (User.IsInRole("DM")) projects = await _context.Projects.Include(m => m.ProjectConfigurator).Include(m => m.ProjectManager).Include(m => m.ProjectGroups).ThenInclude(m => m.Group).ToListAsync();
            else projects = await _context.Projects.Include(m => m.ProjectConfigurator).Include(m => m.ProjectManager).Include(m => m.ProjectGroups).ThenInclude(m => m.Group).Where(m => m.ProjectConfigurator.UserId == me.UserId || m.ProjectManager.UserId == me.UserId).ToListAsync();            
            List<SelectedProjectPoco> returnList = new List<SelectedProjectPoco>();
            foreach (Project p in projects)
            {
                SelectedProjectPoco sup = new SelectedProjectPoco() { myProject = p, myGroup = mgroup };

                returnList.Add(sup);
            }

            string json = JsonConvert.SerializeObject(returnList);
            return Content(json, "application/json");
        }
        public async Task<IActionResult> GetReadonlyUsersByProject(Guid? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            List<ProjectGroup> readOnlyGroups = await _context.ProjectsGroups
                .Include(m => m.Group).ThenInclude(u => u.GroupUsers).ThenInclude(g => g.User)
                .Where(m => m.ReadOnly == true && m.ProjectId == id).ToListAsync();

            
            List<string> usersList = UserHelper.GetAllUsers(Configuration["JWT:Admin-Token-Url"], Configuration["JWT:Admin-Url"], Configuration["JWT:Admin-Key"], Configuration["JWT:Admin-User"], Configuration["JWT:Admin-Password"]);
            List<SelectedUserPoco> returnList = new List<SelectedUserPoco>();
            foreach (string userid in usersList)
            {
                DB.Models.Domain.User u = await _context.Users.FindAsync(userid);
                if (u != null)
                {
                    SelectedUserPoco sup = new SelectedUserPoco() { myUser = u };
                    if (readOnlyGroups.Any(m => m.Group.GroupUsers.Any(z => z.UserId == u.UserId)))
                    {
                        sup.selected = true;
                    }
                    returnList.Add(sup);
                }
            }

            string json = JsonConvert.SerializeObject(returnList);
            return Content(json, "application/json");
        }
        public async Task<IActionResult> EditUsersInGroup([FromBody] Users data)
        {
            var mgroup = await _context.Groups
                .Include(m => m.GroupUsers)
                .FirstOrDefaultAsync(m => m.GroupId == data.guid);
            if (mgroup == null)
            {
                return NotFound();
            }

            foreach (GroupUser guOld in mgroup.GroupUsers)
            {
                _context.GroupsUsers.Remove(guOld);
            }


            foreach (UserIds userid in data.items)
            {
                DB.Models.Domain.User u = await _context.Users.FindAsync(userid.value);
                if (u!=null)
                {
                    GroupUser gu = new GroupUser() { Group = mgroup, User = u };
                    _context.GroupsUsers.Add(gu);
                }

            }


                await _context.SaveChangesAsync();


            return Content("OK", "application/json");
        }
        public async Task <IActionResult> EditReadOnlyUsersInProject([FromBody] Users data)
        {
            Project p = await _context.Projects.FindAsync(data.guid);

            List <ProjectGroup> readOnlyGroups = await _context.ProjectsGroups
                                                 .Include(m => m.Group).ThenInclude(u => u.GroupUsers).ThenInclude(g => g.User)
                                                 .Where(m => m.ReadOnly == true && m.ProjectId == data.guid).ToListAsync();

            if (readOnlyGroups?.Count() == 0)
            {
                Group gr = new Group()
                {
                    GroupName = "Lesende für " + p.ProjectName
                };
                gr.GroupStatusId = GroupStatusEnum.Gruppendaten_gueltig;
                ProjectGroup newReadonly = new ProjectGroup()
                {
                    Group = gr, Project = p, ReadOnly = true
                };
                readOnlyGroups = new List <ProjectGroup>();
                readOnlyGroups.Add(newReadonly);
                _context.ProjectsGroups.Add(newReadonly);
            }
            else
            {
                foreach (GroupUser guOld in readOnlyGroups.First().Group?.GroupUsers)
                {
                    _context.GroupsUsers.Remove(guOld);
                }
            }


            foreach (UserIds userid in data.items)
            {
                DB.Models.Domain.User u = await _context.Users.FindAsync(userid.value);

                if (u != null)
                {
                    GroupUser gu = new GroupUser()
                    {
                        Group = readOnlyGroups.First().Group, User = u
                    };
                    _context.GroupsUsers.Add(gu);
                }
            }


            await _context.SaveChangesAsync();


            return(Content("OK", "application/json"));
        }
Beispiel #6
0
        // GET: Layers
        public async Task <IActionResult> Index()
        {
            DB.Models.Domain.User me        = UserHelper.GetCurrentUser(User, _context);
            List <Layer>          alllayers = await _context.Layers.Include(m => m.LayerUsers).Include(m => m.LayerChangeLogs).ThenInclude(c => c.ChangeLog).ThenInclude(cl => cl.User).Where(m => m.Public == true).ToListAsync();

            alllayers.AddRange(await _context.Layers.Include(m => m.LayerUsers).Include(m => m.LayerChangeLogs).ThenInclude(c => c.ChangeLog).ThenInclude(cl => cl.User).Where(m => m.LayerUsers.Any(u => u.UserId == me.UserId)).ToListAsync());


            List <LayerViewModel> lvms = new List <LayerViewModel>();

            foreach (Layer l in alllayers)
            {
                bool isEditable = true;
                if (!User.IsInRole("DM"))
                {
                    if (l.LayerUsers == null)
                    {
                        isEditable = false;
                    }
                    else if (l.LayerUsers.Where(m => m.UserId == me.UserId).Count() == 0)
                    {
                        ChangeLogLayer chll = l.LayerChangeLogs.Take(1).FirstOrDefault();
                        if (chll?.ChangeLog.User.UserId != me.UserId)
                        {
                            isEditable = false;
                        }
                    }
                }

                LayerViewModel lvm = new LayerViewModel()
                {
                    Layer = l, Editable = isEditable
                };
                lvms.Add(lvm);
            }

            return(View(lvms));
        }