groups.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. use ansi_term::Style;
  2. use users::{Users, Groups};
  3. use crate::fs::fields as f;
  4. use crate::output::cell::TextCell;
  5. use crate::output::table::UserFormat;
  6. impl f::Group {
  7. pub fn render<C: Colours, U: Users+Groups>(self, colours: &C, users: &U, format: UserFormat) -> TextCell {
  8. use users::os::unix::GroupExt;
  9. let mut style = colours.not_yours();
  10. let group = match users.get_group_by_gid(self.0) {
  11. Some(g) => (*g).clone(),
  12. None => return TextCell::paint(style, self.0.to_string()),
  13. };
  14. let current_uid = users.get_current_uid();
  15. if let Some(current_user) = users.get_user_by_uid(current_uid) {
  16. if current_user.primary_group_id() == group.gid()
  17. || group.members().iter().any(|u| u == current_user.name())
  18. {
  19. style = colours.yours();
  20. }
  21. }
  22. let group_name = match format {
  23. UserFormat::Name => group.name().to_string_lossy().into(),
  24. UserFormat::Numeric => group.gid().to_string(),
  25. };
  26. TextCell::paint(style, group_name)
  27. }
  28. }
  29. pub trait Colours {
  30. fn yours(&self) -> Style;
  31. fn not_yours(&self) -> Style;
  32. }
  33. #[cfg(test)]
  34. #[allow(unused_results)]
  35. pub mod test {
  36. use super::Colours;
  37. use crate::fs::fields as f;
  38. use crate::output::cell::TextCell;
  39. use crate::output::table::UserFormat;
  40. use users::{User, Group};
  41. use users::mock::MockUsers;
  42. use users::os::unix::GroupExt;
  43. use ansi_term::Colour::*;
  44. use ansi_term::Style;
  45. struct TestColours;
  46. impl Colours for TestColours {
  47. fn yours(&self) -> Style { Fixed(80).normal() }
  48. fn not_yours(&self) -> Style { Fixed(81).normal() }
  49. }
  50. #[test]
  51. fn named() {
  52. let mut users = MockUsers::with_current_uid(1000);
  53. users.add_group(Group::new(100, "folk"));
  54. let group = f::Group(100);
  55. let expected = TextCell::paint_str(Fixed(81).normal(), "folk");
  56. assert_eq!(expected, group.render(&TestColours, &users, UserFormat::Name));
  57. let expected = TextCell::paint_str(Fixed(81).normal(), "100");
  58. assert_eq!(expected, group.render(&TestColours, &users, UserFormat::Numeric));
  59. }
  60. #[test]
  61. fn unnamed() {
  62. let users = MockUsers::with_current_uid(1000);
  63. let group = f::Group(100);
  64. let expected = TextCell::paint_str(Fixed(81).normal(), "100");
  65. assert_eq!(expected, group.render(&TestColours, &users, UserFormat::Name));
  66. assert_eq!(expected, group.render(&TestColours, &users, UserFormat::Numeric));
  67. }
  68. #[test]
  69. fn primary() {
  70. let mut users = MockUsers::with_current_uid(2);
  71. users.add_user(User::new(2, "eve", 100));
  72. users.add_group(Group::new(100, "folk"));
  73. let group = f::Group(100);
  74. let expected = TextCell::paint_str(Fixed(80).normal(), "folk");
  75. assert_eq!(expected, group.render(&TestColours, &users, UserFormat::Name))
  76. }
  77. #[test]
  78. fn secondary() {
  79. let mut users = MockUsers::with_current_uid(2);
  80. users.add_user(User::new(2, "eve", 666));
  81. let test_group = Group::new(100, "folk").add_member("eve");
  82. users.add_group(test_group);
  83. let group = f::Group(100);
  84. let expected = TextCell::paint_str(Fixed(80).normal(), "folk");
  85. assert_eq!(expected, group.render(&TestColours, &users, UserFormat::Name))
  86. }
  87. #[test]
  88. fn overflow() {
  89. let group = f::Group(2_147_483_648);
  90. let expected = TextCell::paint_str(Fixed(81).normal(), "2147483648");
  91. assert_eq!(expected, group.render(&TestColours, &MockUsers::with_current_uid(0), UserFormat::Numeric));
  92. }
  93. }