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 14namespace BPackageKit { 15 16 17BUser::BUser() 18 : 19 fName(), 20 fRealName(), 21 fHome(), 22 fShell(), 23 fGroups() 24{ 25} 26 27 28BUser::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 41BUser::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 53BUser::~BUser() 54{ 55} 56 57 58status_t 59BUser::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 69const BString& 70BUser::Name() const 71{ 72 return fName; 73} 74 75 76const BString& 77BUser::RealName() const 78{ 79 return fRealName; 80} 81 82 83const BString& 84BUser::Home() const 85{ 86 return fHome; 87} 88 89 90const BString& 91BUser::Shell() const 92{ 93 return fShell; 94} 95 96 97const BStringList& 98BUser::Groups() const 99{ 100 return fGroups; 101} 102 103 104status_t 105BUser::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 119BUser::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