Esempio n. 1
0
        // Returns the file if the user should be able to see it.

        public static UserPathExt GetPath(string uid, User user, string path)
        {
            char SlashType = '/';

            if (path.EndsWith(SlashType))
            {
                path.TrimEnd(SlashType);
            }

            List <UserPath> allowed_paths = Program.config_handler.GetPathPerms(uid, user);

            var PathSplit = path.TrimStart(SlashType).Split(SlashType).ToList();

            if (PathSplit.Count == 0)
            {
                return(null);
            }

            foreach (UserPath usr_path in allowed_paths)
            {
                if (PathSplit[0].ToLower() != usr_path.name.ToLower())
                {
                    continue;
                }

                // This starts with the allowed path.

                PathSplit.RemoveAt(0);
                // Lets remove this part. We only care about the children in this now.

                UserPath end_path = usr_path;
                // This defines the end path.

                bool Inherit = false;
                // Defines whether to inherit permissions.

                while (PathSplit.Count != 0)
                {
                    // This will cycle while the list isn't empty.

                    string NextChild = PathSplit[0];
                    // Defines the next child.

                    bool further = false;
                    // Defines if the for loop actually gets us any further.

                    foreach (UserPath child in end_path.children)
                    {
                        if (child.name.ToLower() == NextChild.ToLower())
                        {
                            // This child is the next match, lets make this the end path and break this for loop.
                            end_path = child;
                            further  = true;
                            break;
                        }
                    }

                    if (!further)
                    {
                        // We are NOT further despite cycling through all of the children, time to break and inherit.
                        Inherit = true;
                        break;
                    }

                    PathSplit.RemoveAt(0);
                    // Removes the first item from the array.
                }

                if (Inherit)
                {
                    // This is where we actually handle inheriting the permissions.
                    end_path = new UserPath()
                    {
                        children      = new List <UserPath>(),
                        delete_folder = end_path.delete_folder,
                        delete_inside = end_path.delete_inside,
                        name          = PathSplit[PathSplit.Count - 1],
                        read          = end_path.read,
                        write         = end_path.write,
                        real_path     = end_path.real_path + SlashType + String.Join(SlashType, PathSplit)
                    };
                }

                UserPathExt path_ext = new UserPathExt()
                {
                    children      = end_path.children,
                    delete_folder = end_path.delete_folder,
                    delete_inside = end_path.delete_inside,
                    name          = end_path.name,
                    read          = end_path.read,
                    real_path     = end_path.real_path,
                    write         = end_path.write,
                    files         = new List <BasicFileFolderInfo>(),
                    folders       = new List <BasicFileFolderInfo>()
                };

                try {
                    string[] Files       = System.IO.Directory.GetFiles(path_ext.real_path, "*", System.IO.SearchOption.TopDirectoryOnly);
                    string[] Directories = System.IO.Directory.GetDirectories(path_ext.real_path, "*", System.IO.SearchOption.TopDirectoryOnly);

                    foreach (string file in Files)
                    {
                        string[] fsplit = file.Split(SlashType);
                        path_ext.files.Add(new BasicFileFolderInfo()
                        {
                            name = fsplit[fsplit.Length - 1],
                            path = file
                        });
                    }

                    foreach (string dir in Directories)
                    {
                        if (dir.EndsWith(SlashType))
                        {
                            dir.TrimEnd(SlashType);
                        }
                        string[] dsplit = dir.Split(SlashType);
                        path_ext.folders.Add(new BasicFileFolderInfo()
                        {
                            name = dsplit[dsplit.Length - 1],
                            path = dir
                        });
                    }
                } catch (Exception) {
                    return(null);
                }

                return(path_ext);
            }

            return(null);
        }
Esempio n. 2
0
        // Gets all of the users.

        public List <UserPath> GetPathPerms(string uid, User user)
        {
            bool administrator          = false;
            UniquehighestPermDict Paths = new UniquehighestPermDict();

            foreach (string group_string in user.groups)
            {
                Group group = config.user_groups[group_string];
                foreach (KeyValuePair <string, PathInfo> path in group.paths)
                {
                    Paths.Add(path.Key, path.Value);
                }
                if (group.administrator)
                {
                    administrator = true;
                }
            }

            if (administrator)
            {
                foreach (string key in Paths.Keys)
                {
                    Paths[key].delete_folder = true;
                    Paths[key].delete_inside = true;
                    Paths[key].read          = true;
                    Paths[key].write         = true;
                }
                var all = new PathInfo()
                {
                    write         = true,
                    read          = true,
                    delete_folder = true,
                    delete_inside = true
                };
                foreach (string group_key in config.user_groups.Keys.Where(p => !user.groups.Any(l => p == l)))
                {
                    var group = config.user_groups[group_key];
                    foreach (string path in group.paths.Keys)
                    {
                        Paths[path] = all;
                    }
                }
                foreach (KeyValuePair <string, User> user_pair in config.users)
                {
                    if (user_pair.Key == uid)
                    {
                        continue;
                    }
                    foreach (string path in user.path_overrides.Keys)
                    {
                        Paths[path] = all;
                    }
                }
            }

            foreach (KeyValuePair <string, PathInfo> path in user.path_overrides)
            {
                Paths.Add(path.Key, path.Value, true);
            }

            if (Paths.Count == 0)
            {
                return(new List <UserPath>());
            }

            Dictionary <int, List <KeyValuePair <string, PathInfo> > > path_lengths = new Dictionary <int, List <KeyValuePair <string, PathInfo> > >();

            char SlashType;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                SlashType = '\\';
            }
            else
            {
                SlashType = '/';
            }

            foreach (KeyValuePair <string, PathInfo> path in Paths)
            {
                try
                {
                    path_lengths[path.Key.Split(SlashType).Length].Add(path);
                }
                catch (Exception)
                {
                    path_lengths[path.Key.Split(SlashType).Length] = new List <KeyValuePair <string, PathInfo> >()
                    {
                        path
                    };
                }
            }

            var keys = path_lengths.Keys.ToList();

            keys.Sort();

            Dictionary <string, UserPath> RawPath_Result = new Dictionary <string, UserPath>();
            var smallest_key          = keys[0];
            var smallest_length_paths = path_lengths[keys[0]];

            keys.RemoveAt(0);
            path_lengths.Remove(smallest_key);

            foreach (KeyValuePair <string, PathInfo> pair in smallest_length_paths)
            {
                var key_split = pair.Key.Split(SlashType);
                RawPath_Result[pair.Key] = new UserPath()
                {
                    children      = new List <UserPath>(),
                    delete_folder = pair.Value.delete_folder,
                    delete_inside = pair.Value.delete_inside,
                    name          = key_split[key_split.Length - 1],
                    read          = pair.Value.read,
                    real_path     = pair.Key,
                    write         = pair.Value.write
                };
            }

            foreach (int list_key in keys)
            {
                List <KeyValuePair <string, PathInfo> > list = path_lengths[list_key];
                foreach (KeyValuePair <string, PathInfo> pair in list)
                {
                    bool cont      = true;
                    var  key_split = pair.Key.Split(SlashType);
                    var  UsrPath   = new UserPath()
                    {
                        children      = new List <UserPath>(),
                        delete_folder = pair.Value.delete_folder,
                        delete_inside = pair.Value.delete_inside,
                        name          = key_split[key_split.Length - 1],
                        read          = pair.Value.read,
                        real_path     = pair.Key,
                        write         = pair.Value.write
                    };
                    foreach (var parent in RawPath_Result.Keys)
                    {
                        if (pair.Key.ToLower().StartsWith(parent.ToLower()))
                        {
                            UserPath pathinfo_ptr = RawPath_Result[parent];
                            while (true)
                            {
                                bool further = false;
                                foreach (UserPath child in pathinfo_ptr.children)
                                {
                                    if (pair.Key.ToLower().StartsWith(child.real_path.ToLower()))
                                    {
                                        further      = true;
                                        pathinfo_ptr = child;
                                        break;
                                    }
                                }
                                if (!further)
                                {
                                    break;
                                }
                            }
                            pathinfo_ptr.children.Add(UsrPath);
                            cont = false;
                            break;
                        }
                    }
                    if (cont)
                    {
                        RawPath_Result[pair.Key] = UsrPath;
                    }
                }
            }

            return(RawPath_Result.Values.ToList());
        }