public UsersManagerWindow(byte[] usersData, byte[] groupsData) { InitializeComponent(); this.usersData = usersData; this.groupsData = groupsData; string[] tmp = UsefulThings.fileFromByteArrToStringArr(groupsData); groups = new List <GroupInfo>(); for (ushort i = 1; i <= tmp.Length; ++i) { GroupInfo gi = new GroupInfo(i, tmp[i - 1]); groups.Add(gi); if (gi.Name[0] != UsefulThings.DELETED_MARK) { groupsListView.Items.Add(gi); } } tmp = UsefulThings.fileFromByteArrToStringArr(usersData); users = new List <UserInfo>(); for (ushort i = 1; i <= tmp.Length; ++i) { string[] tokens = tmp[i - 1].Split(UsefulThings.USERDATA_SEPARATOR.ToString().ToArray(), StringSplitOptions.RemoveEmptyEntries); ushort gid = ushort.Parse(tokens[2]); UserInfo ui = new UserInfo(i, tokens[0], tokens[1], gid, groups[gid - 1].ToString(), (UserInfo.Roles)Enum.Parse(typeof(UserInfo.Roles), tokens[3])); users.Add(ui); if (ui.Login[0] != UsefulThings.DELETED_MARK) { usersListView.Items.Add(ui); } } addUserBtn.IsEnabled = addGroupBtn.IsEnabled = Session.userInfo.Role == UserInfo.Roles.ADMIN; }
private void addUserBtn_Click(object sender, RoutedEventArgs e) { UserInfo ui = new UserInfo((ushort)(users.Count + 1), "Новыйпользователь", 1, groups[0].Name, UserInfo.Roles.USER); EditUserWindow euw = new EditUserWindow(ui, groups.FindAll(item => item.Name[0] != UsefulThings.DELETED_MARK)); euw.changePassChb.IsChecked = true; euw.changePassChb.IsEnabled = false; if (euw.ShowDialog().Value) { string newLogin = euw.loginEdit.Text; if (users.Find(item => item.Login.Equals(newLogin)) != null) { MessageBox.Show("Пользователь с таким логином уже существует", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Warning); } else { SHA1 sha = SHA1.Create(); ui.Login = newLogin; ui.Digest = UsefulThings.ENCODING.GetString(sha.ComputeHash(UsefulThings.ENCODING.GetBytes(euw.pass1Edit.Password))); ui.Digest = UsefulThings.replaceControlChars(ui.Digest); ui.Gid = (euw.groupCB.SelectedItem as GroupInfo).Id; ui.Group = (euw.groupCB.SelectedItem as GroupInfo).Name; ui.Role = (UserInfo.Roles)Enum.Parse(typeof(UserInfo.Roles), euw.roleCB.SelectedItem.ToString()); users.Add(ui); usersListView.Items.Add(ui); } } }
private void onBackLMBUp(object sender, MouseButtonEventArgs e) { string newPath, tmp; UsefulThings.detachLastFilename(fsctrl.CurrDir, out newPath, out tmp); openPath(newPath); }
private void editUserBtn_Click(object sender, RoutedEventArgs e) { UserInfo ui = usersListView.SelectedItem as UserInfo; EditUserWindow euw = new EditUserWindow(ui, groups.FindAll(item => item.Name[0] != UsefulThings.DELETED_MARK)); if (ui.Uid == 1 || Session.userInfo.Role == UserInfo.Roles.USER) { euw.groupCB.IsEnabled = false; euw.roleCB.IsEnabled = false; } if (euw.ShowDialog().Value) { string newLogin = euw.loginEdit.Text; List <UserInfo> namesakes = users.FindAll(item => item.Login.Equals(newLogin)); if (namesakes.Count > 1 || namesakes.Count == 1 && namesakes[0].Uid != (usersListView.SelectedItem as UserInfo).Uid) { MessageBox.Show("Пользователь с таким логином уже существует", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Warning); } else { SHA1 sha = SHA1.Create(); string newDigest = UsefulThings.ENCODING.GetString(sha.ComputeHash(UsefulThings.ENCODING.GetBytes(euw.pass1Edit.Password))); newDigest = UsefulThings.replaceControlChars(newDigest); bool isContinue = true; if ((!newLogin.Equals(ui.Login) || !newDigest.Equals(ui.Digest)) && Session.userInfo.Role == UserInfo.Roles.USER) { bool passEntered; do { AskPasswordWindow apw = new AskPasswordWindow(); passEntered = apw.ShowDialog().Value; string confirmDigest = UsefulThings.ENCODING.GetString(sha.ComputeHash(UsefulThings.ENCODING.GetBytes(apw.passEdit.Password))); confirmDigest = UsefulThings.replaceControlChars(confirmDigest); isContinue = passEntered && confirmDigest.Equals(ui.Digest); if (passEntered && !isContinue) { MessageBox.Show("Прежний пароль введён неверно! Повторите ввод.", "Неверный пароль", MessageBoxButton.OK, MessageBoxImage.Warning); } } while (passEntered && !isContinue); } if (isContinue) { int index = usersListView.SelectedIndex; ui.Login = newLogin; ui.Digest = newDigest; ui.Gid = (euw.groupCB.SelectedItem as GroupInfo).Id; ui.Group = (euw.groupCB.SelectedItem as GroupInfo).Name; ui.Role = (UserInfo.Roles)Enum.Parse(typeof(UserInfo.Roles), euw.roleCB.SelectedItem.ToString()); reloadUsers(); usersListView.SelectedIndex = index; if (Session.userInfo.Uid == ui.Uid) { Session.userInfo = new UserInfo(ui); } } } } }
private void authorize(string login, string password, bool createNew, ContentControl statusControl) { statusControl.Visibility = Visibility.Hidden; FileSystemSettingsWindow fssw = null; if (createNew) { fssw = new FileSystemSettingsWindow(); if (!fssw.ShowDialog().Value) { return; } } FileDialog dialog = createNew ? (FileDialog) new SaveFileDialog() : (FileDialog) new OpenFileDialog(); dialog.DefaultExt = "mfs"; dialog.Filter = "Meow disk (*.mfs)|*.mfs"; UserInfo userInfo = null; if (dialog.ShowDialog() == true) { FileSystemController fsctrl = new FileSystemController(); SHA1 sha = SHA1.Create(); string digest = UsefulThings.ENCODING.GetString(sha.ComputeHash(UsefulThings.ENCODING.GetBytes(password))); digest = UsefulThings.replaceControlChars(digest); bool success; try { if (createNew) { //Создать //TODO 15.11: запрашивать параметры создаваемого диска? /*ushort clusterSize = FileSystemController.FACTOR * 4; //Блок = 4 КБ * ushort rootSize = (ushort)(clusterSize * 10); //Корневой каталог = 10 блоков * uint diskSize = 50 * FileSystemController.FACTOR * FileSystemController.FACTOR; //Раздел = 50 МБ (или 1 МБ для тестов)*/ fsctrl.SuperBlock = new SuperBlock(fsctrl, "MeowFS", fssw.ClusterSizeBytes, fssw.RootSizeBytes, fssw.DiskSizeBytes); fsctrl.Fat = new FAT(fsctrl, (int)(fssw.DiskSizeBytes / fssw.ClusterSizeBytes)); fsctrl.RootDir = UsefulThings.ENCODING.GetBytes(new String('\0', fssw.RootSizeBytes)); fsctrl.createSpace(dialog.FileName, login, digest); userInfo = new UserInfo(1, login, 1, UserInfo.DEFAULT_GROUP, UserInfo.Roles.ADMIN); success = true; } else { //Открыть fsctrl.openSpace(dialog.FileName); byte[] users = fsctrl.readFile("/users.sys", false); string[] usersStr = UsefulThings.fileFromByteArrToStringArr(users); string[] tokens = { "", "", "", "" }; //0 = login, 1 = digest, 2 = gid, 3 = role ushort uid; success = false; for (uid = 1; uid <= usersStr.Length && !success; ++uid) { tokens = usersStr[uid - 1].Split(UsefulThings.USERDATA_SEPARATOR.ToString().ToArray(), StringSplitOptions.None); success = tokens[0].ToLower().Equals(login.ToLower()) && tokens[1].Equals(digest); } if (success) { --uid; byte[] groups = fsctrl.readFile("/groups.sys", false); string[] groupsStr = UsefulThings.fileFromByteArrToStringArr(groups); ushort gid = ushort.Parse(tokens[2]); if (gid > groups.Length) { gid = 1; } userInfo = new UserInfo(uid, tokens[0], gid, groupsStr[gid - 1], (UserInfo.Roles)Enum.Parse(typeof(UserInfo.Roles), tokens[3])); } } } catch { success = false; } finally { fsctrl.closeSpace(); } if (success) { Session.userInfo = userInfo; MainWindow mw = new MainWindow(dialog.FileName); Close(); mw.Show(); } else { statusControl.Content = "Доступ не разрешён"; statusControl.Visibility = Visibility.Visible; } } }
private void loginEdit_TextChanged(object sender, TextChangedEventArgs e) { UsefulThings.controlLettersAndDigits(sender as TextBox); openBtn.IsEnabled = createBtn.IsEnabled = loginEdit.Text.Length > 0; }
private void nameEdit_TextChanged(object sender, TextChangedEventArgs e) { UsefulThings.controlLettersAndDigits(sender as TextBox); okBtn.IsEnabled = nameEdit.Text.Length > 0; }
//private enum PathTypes { PT_ABSOLUTE, PT_RELATIVE }; private void openPath(string path) { path = UsefulThings.clearExcessSeparators(path); try { path = UsefulThings.clearExcessSeparators(path); string tmpPathWithoutLast, tmpLast; UsefulThings.detachLastFilename(path, out tmpPathWithoutLast, out tmpLast); uint newCluster; FileHeader fh; if (path.Equals(fsctrl.CurrDir)) { newCluster = fsctrl.CurrDirCluster; fh = new FileHeader("", "", (byte)FileHeader.FlagsList.FL_DIRECTORY, 0, 0); //требуется лишь только, чтобы у заголовка был флаг "директория" } else if (tmpPathWithoutLast.Equals(fsctrl.CurrDir)) { FileHeader tmpFH = fsctrl.getFileHeader(tmpLast, fsctrl.CurrDirCluster, true); if (tmpFH == null) { throw new InvalidPathException(path); } newCluster = tmpFH.FirstCluster; fh = fsctrl.getFileHeader(tmpLast, fsctrl.CurrDirCluster, true); } else { if (path.Equals("")) { newCluster = fsctrl.SuperBlock.RootOffset / fsctrl.SuperBlock.ClusterSize; } else { FileHeader tmpFH = fsctrl.getFileHeader(path, true); if (tmpFH == null) { throw new InvalidPathException(path); } newCluster = tmpFH.FirstCluster; } fh = fsctrl.getFileHeader(path, true); } if (!path.Equals("") && fh == null) { throw new InvalidPathException(path); } if (path.Equals("") || fh.IsDirectory) { byte[] dir = fsctrl.readFile(path, true); wrapPanel.Children.Clear(); while (dir.Length > 0) { FileHeader currFH = new FileHeader(dir); addFileView(currFH); dir = dir.Skip(FileHeader.SIZE).ToArray(); } fsctrl.CurrDir = path; fsctrl.CurrDirCluster = newCluster; addressEdit.Text = fsctrl.CurrDir.Length > 0 ? fsctrl.CurrDir : "/"; selection = null; if (fsctrl.CurrDir.Equals("")) { backImg.IsEnabled = false; } else { backImg.IsEnabled = true; } } else { FileViewerWindow fvw = new FileViewerWindow(fh, UsefulThings.ENCODING.GetString(fsctrl.readFile(fh, true))); fvw.Title = fh.NamePlusExtensionWithoutZeros; fvw.ShowDialog(); if (fvw.IsChanged && MessageBox.Show("Файл был изменён. Сохранить изменения?", "Подтвердите действие", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { fsctrl.rewriteFile(fsctrl.CurrDir, fh, UsefulThings.ENCODING.GetBytes(fvw.textField.Text), true); } } } catch (Exception e) { MessageBox.Show(e.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); openPath(fsctrl.CurrDir); } }