1 /*
2 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7 #include <package/User.h>
8
9 #include <ctype.h>
10
11 #include <package/hpkg/PackageInfoAttributeValue.h>
12
13
14 namespace BPackageKit {
15
16
BUser()17 BUser::BUser()
18 :
19 fName(),
20 fRealName(),
21 fHome(),
22 fShell(),
23 fGroups()
24 {
25 }
26
27
BUser(const BHPKG::BUserData & userData)28 BUser::BUser(const BHPKG::BUserData& userData)
29 :
30 fName(userData.name),
31 fRealName(userData.realName),
32 fHome(userData.home),
33 fShell(userData.shell),
34 fGroups()
35 {
36 for (size_t i = 0; i < userData.groupCount; i++)
37 fGroups.Add(userData.groups[i]);
38 }
39
40
BUser(const BString & name,const BString & realName,const BString & home,const BString & shell,const BStringList & groups)41 BUser::BUser(const BString& name, const BString& realName, const BString& home,
42 const BString& shell, const BStringList& groups)
43 :
44 fName(name),
45 fRealName(realName),
46 fHome(home),
47 fShell(shell),
48 fGroups(groups)
49 {
50 }
51
52
~BUser()53 BUser::~BUser()
54 {
55 }
56
57
58 status_t
InitCheck() const59 BUser::InitCheck() const
60 {
61 if (fName.IsEmpty())
62 return B_NO_INIT;
63 if (!IsValidUserName(fName))
64 return B_BAD_VALUE;
65 return B_OK;
66 }
67
68
69 const BString&
Name() const70 BUser::Name() const
71 {
72 return fName;
73 }
74
75
76 const BString&
RealName() const77 BUser::RealName() const
78 {
79 return fRealName;
80 }
81
82
83 const BString&
Home() const84 BUser::Home() const
85 {
86 return fHome;
87 }
88
89
90 const BString&
Shell() const91 BUser::Shell() const
92 {
93 return fShell;
94 }
95
96
97 const BStringList&
Groups() const98 BUser::Groups() const
99 {
100 return fGroups;
101 }
102
103
104 status_t
SetTo(const BString & name,const BString & realName,const BString & home,const BString & shell,const BStringList & groups)105 BUser::SetTo(const BString& name, const BString& realName, const BString& home,
106 const BString& shell, const BStringList& groups)
107 {
108 fName = name;
109 fRealName = realName;
110 fHome = home;
111 fShell = shell;
112 fGroups = groups;
113
114 return fGroups.CountStrings() == groups.CountStrings() ? B_OK : B_NO_MEMORY;
115 }
116
117
118 /*static*/ bool
IsValidUserName(const char * name)119 BUser::IsValidUserName(const char* name)
120 {
121 if (name[0] == '\0')
122 return false;
123
124 for (; name[0] != '\0'; name++) {
125 if (!isalnum(name[0]) && name[0] != '_')
126 return false;
127 }
128
129 return true;
130 }
131
132
133 } // namespace BPackageKit
134