@@ -33,11 +33,20 @@ func NewContactService(
3333
3434func (s * ContactService ) GetContactByEmail (ctx context.Context , workspaceID string , email string ) (* domain.Contact , error ) {
3535 var err error
36- ctx , _ , _ , err = s .authService .AuthenticateUserForWorkspace (ctx , workspaceID )
36+ ctx , _ , userWorkspace , err : = s .authService .AuthenticateUserForWorkspace (ctx , workspaceID )
3737 if err != nil {
3838 return nil , fmt .Errorf ("failed to authenticate user: %w" , err )
3939 }
4040
41+ // Check permission for reading contacts
42+ if ! userWorkspace .HasPermission (domain .PermissionResourceContacts , domain .PermissionTypeRead ) {
43+ return nil , domain .NewPermissionError (
44+ domain .PermissionResourceContacts ,
45+ domain .PermissionTypeRead ,
46+ "Insufficient permissions: read access to contacts required" ,
47+ )
48+ }
49+
4150 contact , err := s .repo .GetContactByEmail (ctx , workspaceID , email )
4251 if err != nil {
4352 if strings .Contains (err .Error (), "contact not found" ) {
@@ -52,11 +61,20 @@ func (s *ContactService) GetContactByEmail(ctx context.Context, workspaceID stri
5261
5362func (s * ContactService ) GetContactByExternalID (ctx context.Context , externalID string , workspaceID string ) (* domain.Contact , error ) {
5463 var err error
55- ctx , _ , _ , err = s .authService .AuthenticateUserForWorkspace (ctx , workspaceID )
64+ ctx , _ , userWorkspace , err : = s .authService .AuthenticateUserForWorkspace (ctx , workspaceID )
5665 if err != nil {
5766 return nil , fmt .Errorf ("failed to authenticate user: %w" , err )
5867 }
5968
69+ // Check permission for reading contacts
70+ if ! userWorkspace .HasPermission (domain .PermissionResourceContacts , domain .PermissionTypeRead ) {
71+ return nil , domain .NewPermissionError (
72+ domain .PermissionResourceContacts ,
73+ domain .PermissionTypeRead ,
74+ "Insufficient permissions: read access to contacts required" ,
75+ )
76+ }
77+
6078 contact , err := s .repo .GetContactByExternalID (ctx , externalID , workspaceID )
6179 if err != nil {
6280 if strings .Contains (err .Error (), "contact not found" ) {
@@ -71,11 +89,20 @@ func (s *ContactService) GetContactByExternalID(ctx context.Context, externalID
7189
7290func (s * ContactService ) GetContacts (ctx context.Context , req * domain.GetContactsRequest ) (* domain.GetContactsResponse , error ) {
7391 var err error
74- ctx , _ , _ , err = s .authService .AuthenticateUserForWorkspace (ctx , req .WorkspaceID )
92+ ctx , _ , userWorkspace , err : = s .authService .AuthenticateUserForWorkspace (ctx , req .WorkspaceID )
7593 if err != nil {
7694 return nil , fmt .Errorf ("failed to authenticate user: %w" , err )
7795 }
7896
97+ // Check permission for reading contacts
98+ if ! userWorkspace .HasPermission (domain .PermissionResourceContacts , domain .PermissionTypeRead ) {
99+ return nil , domain .NewPermissionError (
100+ domain .PermissionResourceContacts ,
101+ domain .PermissionTypeRead ,
102+ "Insufficient permissions: read access to contacts required" ,
103+ )
104+ }
105+
79106 response , err := s .repo .GetContacts (ctx , req )
80107 if err != nil {
81108 s .logger .Error (fmt .Sprintf ("Failed to get contacts: %v" , err ))
@@ -87,11 +114,20 @@ func (s *ContactService) GetContacts(ctx context.Context, req *domain.GetContact
87114
88115func (s * ContactService ) DeleteContact (ctx context.Context , email string , workspaceID string ) error {
89116 var err error
90- ctx , _ , _ , err = s .authService .AuthenticateUserForWorkspace (ctx , workspaceID )
117+ ctx , _ , userWorkspace , err : = s .authService .AuthenticateUserForWorkspace (ctx , workspaceID )
91118 if err != nil {
92119 return fmt .Errorf ("failed to authenticate user: %w" , err )
93120 }
94121
122+ // Check permission for writing contacts
123+ if ! userWorkspace .HasPermission (domain .PermissionResourceContacts , domain .PermissionTypeWrite ) {
124+ return domain .NewPermissionError (
125+ domain .PermissionResourceContacts ,
126+ domain .PermissionTypeWrite ,
127+ "Insufficient permissions: write access to contacts required" ,
128+ )
129+ }
130+
95131 if err := s .repo .DeleteContact (ctx , email , workspaceID ); err != nil {
96132 s .logger .WithField ("email" , email ).Error (fmt .Sprintf ("Failed to delete contact: %v" , err ))
97133 return fmt .Errorf ("failed to delete contact: %w" , err )
@@ -106,12 +142,18 @@ func (s *ContactService) BatchImportContacts(ctx context.Context, workspaceID st
106142 }
107143
108144 var err error
109- ctx , _ , _ , err = s .authService .AuthenticateUserForWorkspace (ctx , workspaceID )
145+ ctx , _ , userWorkspace , err : = s .authService .AuthenticateUserForWorkspace (ctx , workspaceID )
110146 if err != nil {
111147 response .Error = fmt .Sprintf ("failed to authenticate user: %v" , err )
112148 return response
113149 }
114150
151+ // Check permission for writing contacts
152+ if ! userWorkspace .HasPermission (domain .PermissionResourceContacts , domain .PermissionTypeWrite ) {
153+ response .Error = "Insufficient permissions: write access to contacts required"
154+ return response
155+ }
156+
115157 // Validate and upsert
116158 for i , contact := range contacts {
117159 now := time .Now ().UTC ()
@@ -159,14 +201,22 @@ func (s *ContactService) UpsertContact(ctx context.Context, workspaceID string,
159201 }
160202
161203 var err error
162- ctx , _ , _ , err = s .authService .AuthenticateUserForWorkspace (ctx , workspaceID )
204+ ctx , _ , userWorkspace , err : = s .authService .AuthenticateUserForWorkspace (ctx , workspaceID )
163205 if err != nil {
164206 operation .Action = domain .UpsertContactOperationError
165207 operation .Error = err .Error ()
166208 s .logger .WithField ("email" , contact .Email ).Error (fmt .Sprintf ("Failed to authenticate user: %v" , err ))
167209 return operation
168210 }
169211
212+ // Check permission for writing contacts
213+ if ! userWorkspace .HasPermission (domain .PermissionResourceContacts , domain .PermissionTypeWrite ) {
214+ operation .Action = domain .UpsertContactOperationError
215+ operation .Error = "Insufficient permissions: write access to contacts required"
216+ s .logger .WithField ("email" , contact .Email ).Error ("Insufficient permissions: write access to contacts required" )
217+ return operation
218+ }
219+
170220 if err := contact .Validate (); err != nil {
171221 operation .Action = domain .UpsertContactOperationError
172222 operation .Error = err .Error ()
0 commit comments