xref: /haiku/src/kits/package/User.cpp (revision 0f4e11e75c244fd61da150011f1eae5fe79fc2a9)
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 
12 namespace BPackageKit {
13 
14 
15 BUser::BUser()
16 	:
17 	fName(),
18 	fRealName(),
19 	fHome(),
20 	fShell(),
21 	fGroups()
22 {
23 }
24 
25 
26 BUser::BUser(const BString& name, const BString& realName, const BString& home,
27 	const BString& shell, const BStringList& groups)
28 	:
29 	fName(name),
30 	fRealName(realName),
31 	fHome(home),
32 	fShell(shell),
33 	fGroups(groups)
34 {
35 }
36 
37 
38 BUser::~BUser()
39 {
40 }
41 
42 
43 status_t
44 BUser::InitCheck() const
45 {
46 	if (fName.IsEmpty())
47 		return B_NO_INIT;
48 	if (!IsValidUserName(fName))
49 		return B_BAD_VALUE;
50 	return B_OK;
51 }
52 
53 
54 const BString&
55 BUser::Name() const
56 {
57 	return fName;
58 }
59 
60 
61 const BString&
62 BUser::RealName() const
63 {
64 	return fRealName;
65 }
66 
67 
68 const BString&
69 BUser::Home() const
70 {
71 	return fHome;
72 }
73 
74 
75 const BString&
76 BUser::Shell() const
77 {
78 	return fShell;
79 }
80 
81 
82 const BStringList&
83 BUser::Groups() const
84 {
85 	return fGroups;
86 }
87 
88 
89 status_t
90 BUser::SetTo(const BString& name, const BString& realName, const BString& home,
91 	const BString& shell, const BStringList& groups)
92 {
93 	fName = name;
94 	fRealName = realName;
95 	fHome = home;
96 	fShell = shell;
97 	fGroups = groups;
98 
99 	return fGroups.CountStrings() == groups.CountStrings() ? B_OK : B_NO_MEMORY;
100 }
101 
102 
103 /*static*/ bool
104 BUser::IsValidUserName(const char* name)
105 {
106 	if (name[0] == '\0')
107 		return false;
108 
109 	for (; name[0] != '\0'; name++) {
110 		if (!isalnum(name[0]) && name[0] != '_')
111 			return false;
112 	}
113 
114 	return true;
115 }
116 
117 
118 }	// namespace BPackageKit
119