xref: /haiku/src/add-ons/kernel/file_systems/netfs/server/UserSecurityContext.cpp (revision 8a990d5228b2d1099e3062180532ba709dfeef6d)
1 // UserSecurityContext.cpp
2 
3 #include <new>
4 
5 #include <HashMap.h>
6 
7 #include "SecurityContext.h"
8 #include "UserSecurityContext.h"
9 #include "Node.h"
10 
11 // PermissionMap
12 struct UserSecurityContext::PermissionMap : HashMap<NodeRef, Permissions> {
13 };
14 
15 // constructor
16 UserSecurityContext::UserSecurityContext()
17 	: fUser(NULL),
18 	  fPermissions(NULL)
19 {
20 }
21 
22 // destructor
23 UserSecurityContext::~UserSecurityContext()
24 {
25 	if (fUser)
26 		fUser->ReleaseReference();
27 
28 	delete fPermissions;
29 }
30 
31 // Init
32 status_t
33 UserSecurityContext::Init(User* user)
34 {
35 	if (fUser)
36 		fUser->ReleaseReference();
37 
38 	fUser = user;
39 	if (fUser)
40 		fUser->AcquireReference();
41 
42 	delete fPermissions;
43 	fPermissions = new(std::nothrow) PermissionMap;
44 
45 	if (!fPermissions)
46 		return B_NO_MEMORY;
47 	return B_OK;
48 }
49 
50 // GetUser
51 User*
52 UserSecurityContext::GetUser() const
53 {
54 	return fUser;
55 }
56 
57 // AddNode
58 status_t
59 UserSecurityContext::AddNode(dev_t volumeID, ino_t nodeID,
60 	Permissions permissions)
61 {
62 	return fPermissions->Put(NodeRef(volumeID, nodeID), permissions);
63 }
64 
65 // GetNodePermissions
66 Permissions
67 UserSecurityContext::GetNodePermissions(dev_t volumeID, ino_t nodeID) const
68 {
69 	return fPermissions->Get(NodeRef(volumeID, nodeID));
70 }
71 
72 // GetNodePermissions
73 Permissions
74 UserSecurityContext::GetNodePermissions(const node_ref& ref) const
75 {
76 	return fPermissions->Get(NodeRef(ref));
77 }
78 
79 // GetNodePermissions
80 Permissions
81 UserSecurityContext::GetNodePermissions(dev_t volumeID, ino_t nodeID,
82 	Permissions parentPermissions) const
83 {
84 	if (fPermissions->ContainsKey(NodeRef(volumeID, nodeID)))
85 		return fPermissions->Get(NodeRef(volumeID, nodeID));
86 	return parentPermissions;
87 }
88 
89 // GetNodePermissions
90 Permissions
91 UserSecurityContext::GetNodePermissions(const node_ref& ref,
92 	Permissions parentPermissions) const
93 {
94 	if (fPermissions->ContainsKey(NodeRef(ref)))
95 		return fPermissions->Get(NodeRef(ref));
96 	return parentPermissions;
97 }
98 
99