Ejemplo n.º 1
0
        protected void Setup_Tabs()
        {
            //OK, HOW THIS WORKS.
            //We loop trough all roles. per function of the system the user has access to we add a number that is a bitwise new number ( 0,1,2,4,8,16,32,64,128,...)
            //after we've looped trough the roles, we do a bitwise or so we can easily tell if the user has access to a part of the system

            User user = null;

            if (LoggedInUser.IsUserLoggedIn)
            {
                user = LoggedInUser.GetUser();
            }

            var roles = RoleManager.GetUserPermissions(user);

            //handle the tasks tab
            if (!roles.Any(x => x == RolesPermissions.Tasks))
            {
                TasksTab.Visible = false;
                Tasks.Visible    = false;
            }
            else
            {
                //link the departments drop down
                var list = SystemLists.General.Departments;

                var DepartmentsList = new ObservableCollection <Department>();
                DepartmentsList.Add(new Department(0)
                {
                    Description = LanguageFiles.GetLocalTranslation("AllDepartments", "All")
                });
                foreach (var item in SystemLists.General.Departments)
                {
                    DepartmentsList.Add(item);
                }

                DropDownSorting.DataSource = DepartmentsList;
                DropDownSorting.DataBind();

                if (LoggedInUser.IsUserLoggedIn && (!roles.Any(x => x == RolesPermissions.Technician)))
                {
                    DropDownSorting.SelectedIndex =
                        DepartmentsList.ToList().FindIndex(x => x.ID == LoggedInUser.GetUser().Department.ID);
                }
                else
                {
                    DropDownSorting.SelectedIndex = 0;
                }


                //set the parameters for the datasource
                string departmentID = Request.QueryString["depID"];
                var    searchText   = Request.QueryString["Search"];

                var departmentField = TaskSource.SelectParameters["DepartmentID"];
                var searchField     = TaskSource.SelectParameters["SearchText"];
                if (LoggedInUser.IsUserLoggedIn)
                {
                    if (
                        !RoleManager.UserHasPermission(LoggedInUser.GetUser(), RolesPermissions.ManageTasks) &&
                        (
                            (String.IsNullOrWhiteSpace(departmentID) && String.IsNullOrWhiteSpace(searchText)) ||
                            (LoggedInUser.GetUser().Department.ID.ToString() == departmentID)
                        ))
                    {
                        // logged in + either nothing was given or the user's department was chosen -> show department + user's tasks
                        departmentField.DefaultValue = LoggedInUser.GetUser().Department.ID.ToString();
                        searchText = LoggedInUser.GetUser().UserName;
                    }
                    else if (!String.IsNullOrWhiteSpace(departmentID))
                    {
                        //department was set.
                        departmentField.DefaultValue = departmentID;
                    }

                    if (!String.IsNullOrWhiteSpace(searchText))
                    {
                        //search text was given.
                        searchField.DefaultValue = searchText;
                    }
                }
                else
                {
                    if (!String.IsNullOrWhiteSpace(departmentID))
                    {
                        departmentField.DefaultValue = departmentID;
                    }

                    if (!String.IsNullOrWhiteSpace(searchText))
                    {
                        searchField.DefaultValue = searchText;
                    }
                }

                //set the department parameter
                TaskSource.SelectParameters["DepartmentID"] = departmentField;
                TaskSource.SelectParameters["SearchText"]   = searchField;

                //set datasource and bind/retrieve data (databind also executes all inline code to bind to them)
                TaskSource.DataBind();
                TaskGrid.DataSourceID = nameof(TaskSource);
                TaskGrid.DataBind();
            }

            //handle machines tab
            //if (!roles.Any(x => x == RolesPermissions.ManageMachines))
            {
                //no permissions!
                MachinesTab.Visible = false;
                //Machines.Visible = false;
            }

            /*else
             * {
             *  MachinesTab.Visible = true;
             *  //Machines.Visible = true;
             * }*/

            //permissions to the Suppliers tab!

            /*if (!roles.Any(x => x == RolesPermissions.ManageSuppliers) &&
             *  !roles.Any(x => x == RolesPermissions.ViewSuppliers))*/
            {
                SuppliersTab.Visible = false;
                //Suppliers.Visible = false;
            }

            /*else
             * {
             *  SuppliersTab.Visible = true;
             *  Suppliers.Visible = true;
             * }*/


            //permissions for the Users tab!
            if (!roles.Any(x => x == RolesPermissions.ManageUsers))
            {
                UsersTab.Visible = false;
                Users.Visible    = false;
            }
            else
            {
                UsersTab.Visible = true;
                Users.Visible    = true;

                var RolesList = new ObservableCollection <RoleModel>();
                var roleList  = LanguageFiles.LoadLanguageFile("Roles");

                for (int i = 0; i < roleList.Length; i++)
                {
                    RolesList.Add(RoleModel.CreateModel((Role)i, roleList[i]));
                }

                //bind User dropdown of roles
                selectUserType.DataSource = RolesList;
                selectUserType.DataBind();

                //set the parameters for the datasource
                string role       = Request.QueryString["UserRole"];
                var    searchText = Request.QueryString["SearchUser"];

                if (!String.IsNullOrWhiteSpace(role) && role != nameof(Role.AllRoles))
                {
                    var RoleField = UserSource.SelectParameters["role"];
                    RoleField.DefaultValue = role;
                    UserSource.SelectParameters["role"] = RoleField;
                }

                if (!String.IsNullOrWhiteSpace(searchText))
                {
                    var searchField = UserSource.SelectParameters["contains"];
                    searchField.DefaultValue = searchText;
                    UserSource.SelectParameters["contains"] = searchField;
                }

                UserSource.DataBind();
                UserGrid.DataSourceID = nameof(UserSource);
                UserGrid.DataBind();
            }

            //set the last known open tab
            if (!String.IsNullOrWhiteSpace(ActiveTab))
            {
                hidTABControl.Value = ActiveTab;
            }
        }