Skip to content

Commit 250970a

Browse files
committed
feat: user scalation notification
Change-Id: Ie86383e1695f955e304d9a292e350d08731b8144
1 parent 1a7ee28 commit 250970a

9 files changed

Lines changed: 484 additions & 37 deletions

File tree

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php namespace App\Jobs;
2+
/*
3+
* Copyright 2025 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
15+
16+
use Illuminate\Bus\Queueable;
17+
use Illuminate\Contracts\Queue\ShouldQueue;
18+
use Illuminate\Foundation\Bus\Dispatchable;
19+
use Illuminate\Queue\InteractsWithQueue;
20+
use Illuminate\Queue\SerializesModels;
21+
use Illuminate\Support\Facades\Log;
22+
use models\exceptions\ValidationException;
23+
use OpenId\Services\IUserService;
24+
25+
/**
26+
* Class NotifyMonitoredSecurityGroupActivity
27+
* @package App\Jobs
28+
*/
29+
final class NotifyMonitoredSecurityGroupActivity implements ShouldQueue
30+
{
31+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
32+
33+
const ACTION_ADD_2_GROUP = 'Added';
34+
const REMOVE_FROM_GROUP = 'Removed';
35+
36+
const ValidActions = [
37+
self::ACTION_ADD_2_GROUP,
38+
self::REMOVE_FROM_GROUP
39+
];
40+
41+
public $tries = 1;
42+
43+
public $timeout = 0;
44+
45+
/**
46+
* @var string
47+
*/
48+
public $action;
49+
50+
/**
51+
* @var int
52+
*/
53+
public $user_id;
54+
55+
/**
56+
* @var string
57+
*/
58+
public $user_email;
59+
60+
/*
61+
* @var string
62+
*/
63+
public $user_name;
64+
65+
/**
66+
* @var int
67+
*/
68+
public $group_id;
69+
70+
/**
71+
* @var string
72+
*/
73+
public $group_name;
74+
75+
/**
76+
* @var string
77+
*/
78+
public $group_slug;
79+
80+
/**
81+
* @param string $action
82+
* @param int $user_id
83+
* @param string $user_email
84+
* @param string $user_name
85+
* @param int $group_id
86+
* @param string $group_name
87+
* @param string $group_slug
88+
* @throws ValidationException
89+
*/
90+
public function __construct
91+
(
92+
string $action,
93+
int $user_id,
94+
string $user_email,
95+
string $user_name,
96+
int $group_id,
97+
string $group_name,
98+
string $group_slug
99+
)
100+
{
101+
if(!in_array($action, self::ValidActions)){
102+
throw new ValidationException(sprintf("Invalid action %s, valid actions are %s", $action, implode(',', self::ValidActions)));
103+
}
104+
$this->action = $action;
105+
$this->user_id = $user_id;
106+
$this->user_email = $user_email;
107+
$this->user_name = $user_name;
108+
$this->group_id = $group_id;
109+
$this->group_name = $group_name;
110+
$this->group_slug = $group_slug;
111+
112+
Log::debug
113+
(
114+
sprintf
115+
(
116+
"NotifyMonitoredSecurityGroupActivity::constructor action %s user_id %s user_email %s user_name %s group_id %s group_name %s group_slug %s",
117+
$action,
118+
$user_id,
119+
$user_email,
120+
$user_name,
121+
$group_id,
122+
$group_name,
123+
$group_slug
124+
)
125+
);
126+
}
127+
128+
/**
129+
* @param IUserService $service
130+
* @return void
131+
*/
132+
public function handle(IUserService $service){
133+
Log::debug
134+
(
135+
sprintf
136+
(
137+
"NotifyMonitoredSecurityGroupActivity::handle action %s user_id %s user_email %s user_name %s group_id %s group_name %s group_slug %s",
138+
$this->action,
139+
$this->user_id,
140+
$this->user_email,
141+
$this->user_name,
142+
$this->group_id,
143+
$this->group_name,
144+
$this->group_slug
145+
)
146+
);
147+
148+
$service->notifyMonitoredSecurityGroupActivity
149+
(
150+
$this->action,
151+
$this->user_id,
152+
$this->user_email,
153+
$this->user_name,
154+
$this->group_id,
155+
$this->group_name,
156+
$this->group_slug
157+
);
158+
159+
}
160+
161+
public function failed(\Throwable $exception)
162+
{
163+
Log::error(sprintf( "NotifyMonitoredSecurityGroupActivity::failed %s", $exception->getMessage()));
164+
}
165+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php namespace App\Mail;
2+
/*
3+
* Copyright 2025 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
15+
16+
use Illuminate\Bus\Queueable;
17+
use Illuminate\Mail\Mailable;
18+
use Illuminate\Queue\SerializesModels;
19+
use Illuminate\Support\Facades\Config;
20+
use Illuminate\Support\Facades\Log;
21+
22+
/**
23+
* Class MonitoredSecurityGroupNotificationEmail
24+
* @package App\Mail
25+
*/
26+
final class MonitoredSecurityGroupNotificationEmail extends Mailable
27+
{
28+
use Queueable, SerializesModels;
29+
30+
public $tries = 3;
31+
32+
/**
33+
* @var string
34+
*/
35+
public $action;
36+
37+
/**
38+
* @var int
39+
*/
40+
public $user_id;
41+
42+
/**
43+
* @var string
44+
*/
45+
public $user_email;
46+
47+
/*
48+
* @var string
49+
*/
50+
public $user_name;
51+
52+
/**
53+
* @var int
54+
*/
55+
public $group_id;
56+
57+
/**
58+
* @var string
59+
*/
60+
public $group_name;
61+
62+
/**
63+
* @var string
64+
*/
65+
public $group_slug;
66+
67+
/**
68+
* @var string
69+
*/
70+
public $email;
71+
72+
/**
73+
* @param string $email
74+
* @param string $action
75+
* @param int $user_id
76+
* @param string $user_email
77+
* @param string $user_name
78+
* @param int $group_id
79+
* @param string $group_name
80+
* @param string $group_slug
81+
*/
82+
public function __construct
83+
(
84+
string $email,
85+
string $action,
86+
int $user_id,
87+
string $user_email,
88+
string $user_name,
89+
int $group_id,
90+
string $group_name,
91+
string $group_slug
92+
)
93+
{
94+
$this->email = $email;
95+
$this->action = $action;
96+
$this->user_id = $user_id;
97+
$this->user_email = $user_email;
98+
$this->user_name = $user_name;
99+
$this->group_id = $group_id;
100+
$this->group_name = $group_name;
101+
$this->group_slug = $group_slug;
102+
103+
Log::debug
104+
(
105+
sprintf
106+
(
107+
"MonitoredSecurityGroupNotificationEmail::constructor email %s action %s user_id %s user_email %s user_name %s group_id %s group_name %s group_slug %s",
108+
$email,
109+
$action,
110+
$user_id,
111+
$user_email,
112+
$user_name,
113+
$group_id,
114+
$group_name,
115+
$group_slug
116+
)
117+
);
118+
}
119+
120+
public function build()
121+
{
122+
$this->subject = sprintf
123+
(
124+
"[%s] User %s (%s) was %s from group %s (%s)"
125+
,Config::get('app.app_name')
126+
,$this->user_name
127+
,$this->user_email
128+
,$this->action
129+
,$this->group_name
130+
,$this->group_id
131+
);
132+
Log::debug(sprintf("MonitoredSecurityGroupNotificationEmail::build to %s", $this->email));
133+
return $this->from(Config::get("mail.from"))
134+
->to($this->email)
135+
->subject($this->subject)
136+
->view('emails.audit.monitored_security_group_notification');
137+
}
138+
}

app/Services/Auth/UserService.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,25 @@ public function registerUser(array $payload, ?OAuth2OTP $otp = null):User
143143
)
144144
);
145145

146+
147+
148+
$user = UserFactory::build($payload);
146149
$default_groups = $this->group_repository->getDefaultOnes();
147150
if (count($default_groups) > 0) {
148-
$payload['groups'] = $default_groups;
151+
foreach ($default_groups as $group) {
152+
Log::debug
153+
(
154+
sprintf
155+
(
156+
"UserService::registerUser assigning user %s to default groups %s",
157+
$user->getEmail(),
158+
$group->getSlug()
159+
)
160+
);
161+
$user->addToGroup($group);
162+
}
149163
}
150164

151-
$user = UserFactory::build($payload);
152165
$this->identifier_service->generateIdentifier($user);
153166

154167
if(!is_null($otp))

0 commit comments

Comments
 (0)