@@ -11,6 +11,16 @@ import io.github.gitbucket.markedj.Marked
11
11
import io .github .gitbucket .markedj .Options
12
12
import org .slf4j .LoggerFactory
13
13
14
+ import gitbucket .core .model .{GroupMember , Account }
15
+ import gitbucket .core .model .Profile ._
16
+ import gitbucket .core .util .{StringUtil , LDAPUtil }
17
+ import gitbucket .core .service .SystemSettingsService .SystemSettings
18
+ import profile .simple ._
19
+ import StringUtil ._
20
+ import org .slf4j .LoggerFactory
21
+ // TODO Why is direct import required?
22
+ import gitbucket .core .model .Profile .dateColumnType
23
+
14
24
class AnnounceController extends AnnounceControllerBase
15
25
with AdminAuthenticator
16
26
@@ -19,11 +29,30 @@ trait AnnounceControllerBase extends ControllerBase with AccountService {
19
29
20
30
private val logger = LoggerFactory .getLogger(classOf [AnnounceController ])
21
31
22
- case class AnnounceForm (content : String , subject : String )
32
+ object EmailAddress {
33
+ def isValid (email : String ): Boolean = EmailRegex .pattern.matcher(email.toUpperCase).matches()
34
+
35
+ private val EmailRegex = """ \b[a-zA-Z0-9.!#$%&¡¯*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*\b""" .r
36
+ }
37
+
38
+ def getAccountByGroupName (groupName : String , includeRemoved : Boolean = false )(implicit s : Session ): List [Account ] = {
39
+ val needs = GroupMembers
40
+ .filter(_.groupName === groupName.bind)
41
+ .sortBy(_.userName)
42
+ .map(_.userName)
43
+ .list
44
+
45
+ Accounts
46
+ .filter(t => (t.userName inSetBind needs) && (t.removed === false .bind, ! includeRemoved))
47
+ .list
48
+ }
49
+
50
+ case class AnnounceForm (content : String , subject : String , to : String )
23
51
24
52
private val announceForm = mapping(
25
53
" content" -> trim(label(" Announce" , text(required))),
26
- " subject" -> trim(label(" Subject" , text(required)))
54
+ " subject" -> trim(label(" Subject" , text(required))),
55
+ " to" -> trim(label(" To" , text(required)))
27
56
)(AnnounceForm .apply)
28
57
29
58
get(" /admin/announce" )(adminOnly {
@@ -63,10 +92,19 @@ trait AnnounceControllerBase extends ControllerBase with AccountService {
63
92
64
93
email.setHtmlMsg(Marked .marked(form.content, opts))
65
94
66
- logger.info(" sending email: {}" , form.content)
95
+ logger.debug(" sending email subject: {}" , form.subject)
96
+ logger.debug(" sending email content: {}" , form.content)
67
97
val database = Database ()
68
98
database withSession { implicit session =>
69
- getAllUsers(false ).filter(account => ! account.isGroupAccount && account.mailAddress.nonEmpty).foreach(account => email.addBcc(account.mailAddress))
99
+ val mailto = form.to.split(" ," ).map(_.trim).map(groupaccount => {
100
+ val userMailAddress : List [Account ] = if (groupaccount.toUpperCase == " ALL" ) getAllUsers(false ) else getAccountByGroupName(groupaccount)
101
+ userMailAddress.filter(account => ! account.isGroupAccount && account.mailAddress.nonEmpty && EmailAddress .isValid(account.mailAddress))
102
+ }
103
+ ).flatMap(a => a).map(_.mailAddress).toSet// .foreach(account => email.addBcc(account.mailAddress)
104
+ logger.debug(" sending email to: {}" , form.to)
105
+ logger.debug(" sending email to EmailAddress: {}" , mailto.mkString(" ," ))
106
+ // getAllUsers(false).filter(account => !account.isGroupAccount && account.mailAddress.nonEmpty && EmailAddress.isValid(account.mailAddress)).foreach(account => email.addBcc(account.mailAddress))
107
+ mailto.foreach(mailAddress => email.addBcc(mailAddress))
70
108
}
71
109
72
110
email.send()
0 commit comments