Class: Notification

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Resource
Defined in:
models/notification.rb

Overview

Resource representing a Notification to a User about a Comment they should view or an action they need to take. Notifications are displayed in UI and do not represent email sent by Vulnreport.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from DataMapper::Resource

#make_dirty

Class Method Details

+ (Array<Notification>) allForUser(uid)

Get all Notifications for a User

Parameters:

  • uid (Integer)

    ID of User to get Notifications for

Returns:

  • (Array<Notification>)

    All Notifications for given User



122
123
124
# File 'models/notification.rb', line 122

def self.allForUser(uid)
	return all(:uidToNotify => uid, :order => [:id.desc])
end

+ (String) body(repository = nil)

Returns Body/text of this Notification

Returns:

  • (String)

    Body/text of this Notification



19
# File 'models/notification.rb', line 19

property :body,				Text, :length => 2048

+ (DateTime) created_at(repository = nil)

Returns Date/Time Notification created (DM Handled)

Returns:

  • (DateTime)

    Date/Time Notification created (DM Handled)



21
# File 'models/notification.rb', line 21

property :created_at, 		DateTime

+ (Array<Notification>) forUser(uid)

Get all unseen Notifications for a User

Parameters:

  • uid (Integer)

    ID of User to get new Notifications for

Returns:

  • (Array<Notification>)

    Unseen Notifications for given User



114
115
116
# File 'models/notification.rb', line 114

def self.forUser(uid)
	return all(:uidToNotify => uid, :viewed => false, :order => [:id.desc])
end

+ (Integer) id(repository = nil)

Returns Primary Key

Returns:

  • (Integer)

    Primary Key



14
# File 'models/notification.rb', line 14

property :id,				Serial

+ (Array<Notification>) markAllUserRead(uid)

Mark all Notifications for a given User as viewed

Parameters:

  • uid (Integer)

    User ID

Returns:

  • (Array<Notification>)

    All Notifications for User that were marked as viewed



140
141
142
143
144
# File 'models/notification.rb', line 140

def self.markAllUserRead(uid)
	ns = all(:uidToNotify => uid, :viewed => false)
	ns.update(:viewed => true)
	return ns
end

+ (Boolean) markRead(nid)

Mark a Notification as viewed

Parameters:

  • nid (Integer)

    Notification ID

Returns:

  • (Boolean)

    True if successful, false otherwise



130
131
132
133
134
# File 'models/notification.rb', line 130

def self.markRead(nid)
	n = get(nid)
	n.viewed = true
	return n.save
end

+ (NOTIF_CLASS) notifClass(repository = nil)

Returns Type of Notification

Returns:



16
# File 'models/notification.rb', line 16

property :notifClass,		Integer

+ (DateTime) renewed_at(repository = nil)

Returns If a second notification for same what/whatId is logged, set this at that Date/Time

Returns:

  • (DateTime)

    If a second notification for same what/whatId is logged, set this at that Date/Time



22
# File 'models/notification.rb', line 22

property :renewed_at,		DateTime

+ (Integer) renewed_count(repository = nil)

Returns Number of times this Notification has been renewed

Returns:

  • (Integer)

    Number of times this Notification has been renewed



23
# File 'models/notification.rb', line 23

property :renewed_count,	Integer, :default => 0

+ (Integer) uidToNotify(repository = nil)

Returns ID of User this Notification applies to

Returns:

  • (Integer)

    ID of User this Notification applies to



15
# File 'models/notification.rb', line 15

property :uidToNotify,		Integer

+ (Boolean) viewed(repository = nil)

Returns True if this Notification has been viewed by User

Returns:

  • (Boolean)

    True if this Notification has been viewed by User



20
# File 'models/notification.rb', line 20

property :viewed,			Boolean, :default => false

+ (DateTime) viewed_at(repository = nil)

Returns Date/Time Notification was viewed by User

Returns:

  • (DateTime)

    Date/Time Notification was viewed by User



24
# File 'models/notification.rb', line 24

property :viewed_at,		DateTime

+ (LINK_TYPE) what(repository = nil)

Returns Type of resource this Notification is attached to

Returns:

  • (LINK_TYPE)

    Type of resource this Notification is attached to



17
# File 'models/notification.rb', line 17

property :what,				Integer

+ (Integer) whatId(repository = nil)

Returns ID of resource this Notification is attached to

Returns:

  • (Integer)

    ID of resource this Notification is attached to



18
# File 'models/notification.rb', line 18

property :whatId,			Integer

Instance Method Details

- (String) body

Returns Body/text of this Notification

Returns:

  • (String)

    Body/text of this Notification



19
# File 'models/notification.rb', line 19

property :body,				Text, :length => 2048

- (String) body=(value)

Returns Body/text of this Notification

Returns:

  • (String)

    Body/text of this Notification



19
# File 'models/notification.rb', line 19

property :body,				Text, :length => 2048

- (Object) create

Before creating the Notification, check if it should instead be a renewal (same UID, What, and WhatID for an Notification not yet viewed). If it should be a renewal, update that existing Notification. Otherwise, create a notification. As part of that, generate the Notification body based on what object it is being attached to and the class of Notification (NOTIF_CLASS).



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'models/notification.rb', line 30

before :create do
	existing = Notification.first(:uidToNotify => self.uidToNotify, :what => self.what, :whatId => self.whatId, :viewed => false)
	if(!existing.nil?)
		existing.renewed_at = DateTime.now
		existing.renewed_count += 1
		existing.save
		throw :halt
	end

	if(!self.notifClass.nil? && self.body.nil?)
		whatStr = nil
		if(self.what == LINK_TYPE::APPLICATION)
			app = Application.get(self.whatId)
			whatStr = app.name
		elsif(self.what == LINK_TYPE::TEST)
			test = Test.get(self.whatId)
			whatStr = test.name + " - " + test.application.name
		elsif(self.what == LINK_TYPE::VULN)
			vuln = Vulnerability.get(self.whatId)
			whatStr = vuln.type_str + " on " + vuln.test.name + " - " + vuln.test.application.name
		end

		if(self.notifClass == NOTIF_CLASS::REPLY_TO_COMMENT)
			if(!whatStr.nil?)
				self.body = "New reply on '#{whatStr}'"
			else
				self.body = "New reply to comment"
			end
		elsif(self.notifClass == NOTIF_CLASS::COMMENT_APP)
			if(!whatStr.nil?)
				self.body = "New comment on an Application you've tested - '#{whatStr}'"
			else
				self.body = "New comment on an Application you've tested"
			end
		elsif(self.notifClass == NOTIF_CLASS::COMMENT_TEST)
			if(!whatStr.nil?)
				self.body = "New comment on your test - '#{whatStr}'"
			else
				self.body = "New comment on one of your tests"
			end
		elsif(self.notifClass == NOTIF_CLASS::COMMENT_VULN)
			if(!whatStr.nil?)
				self.body = "New comment on a vuln you created - '#{whatStr}'"
			else
				self.body = "New comment on a vuln you created"
			end
		elsif(self.notifClass == NOTIF_CLASS::COMMENT_APP_APPROVER)
			if(!whatStr.nil?)
				self.body = "New comment on an Application you've approved a test for - '#{whatStr}'"
			else
				self.body = "New comment on an Application you've approved a test for"
			end
		elsif(self.notifClass == NOTIF_CLASS::COMMENT_TEST_APPROVER)
			if(!whatStr.nil?)
				self.body = "New comment on a test you approved - '#{whatStr}'"
			else
				self.body = "New comment on a test you approved"
			end
		elsif(self.notifClass == NOTIF_CLASS::COMMENT_VULN_APPROVER)
			if(!whatStr.nil?)
				self.body = "New comment on a vuln for a test you approved - '#{whatStr}'"
			else
				self.body = "New comment on a vuln for a test you approved"
			end
		elsif(self.notifClass == NOTIF_CLASS::PROV_PASS_REQUEST)
			if(!whatStr.nil?)
				self.body = "Provisional pass requested for '#{whatStr}'"
			else
				self.body = "Provisional pass requested"
			end
		elsif(self.notifClass == NOTIF_CLASS::PROV_PASS_APPROVE)
			if(!whatStr.nil?)
				self.body = "Your provisional pass request for '#{whatStr}' has been approved"
			else
				self.body = "Your provisional pass has been approved"
			end
		end
	end
end

- (DateTime) created_at

Returns Date/Time Notification created (DM Handled)

Returns:

  • (DateTime)

    Date/Time Notification created (DM Handled)



21
# File 'models/notification.rb', line 21

property :created_at, 		DateTime

- (DateTime) created_at=(value)

Returns Date/Time Notification created (DM Handled)

Returns:

  • (DateTime)

    Date/Time Notification created (DM Handled)



21
# File 'models/notification.rb', line 21

property :created_at, 		DateTime

- (Integer) id

Returns Primary Key

Returns:

  • (Integer)

    Primary Key



14
# File 'models/notification.rb', line 14

property :id,				Serial

- (Integer) id=(value)

Returns Primary Key

Returns:

  • (Integer)

    Primary Key



14
# File 'models/notification.rb', line 14

property :id,				Serial

The HREF for this Notification to link to

Returns:

  • (String)

    HREF to use in a link for this Notification



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'models/notification.rb', line 149

def link
	#Separate these out so they dont get the fromNotif arg that auto-opens comments
	if(self.notifClass == NOTIF_CLASS::PROV_PASS_REQUEST || self.notifClass == NOTIF_CLASS::PROV_PASS_APPROVE)
		return "/reviews/#{self.whatId}" if(self.what == LINK_TYPE::APPLICATION)
		return "/tests/#{self.whatId}" if(self.what == LINK_TYPE::TEST)
	end

	return "/reviews/#{self.whatId}?fromNotif=1" if(self.what == LINK_TYPE::APPLICATION)
	return "/tests/#{self.whatId}?fromNotif=1" if(self.what == LINK_TYPE::TEST)
	if(self.what == LINK_TYPE::VULN)
		v = Vulnerability.get(self.whatId)
		return "" if(v.nil?)
		return "/tests/#{v.test.id}/#{self.whatId}?fromNotif=1"
	end

	return nil
end

- (NOTIF_CLASS) notifClass

Returns Type of Notification

Returns:



16
# File 'models/notification.rb', line 16

property :notifClass,		Integer

- (NOTIF_CLASS) notifClass=(value)

Returns Type of Notification

Returns:



16
# File 'models/notification.rb', line 16

property :notifClass,		Integer

- (DateTime) renewed_at

Returns If a second notification for same what/whatId is logged, set this at that Date/Time

Returns:

  • (DateTime)

    If a second notification for same what/whatId is logged, set this at that Date/Time



22
# File 'models/notification.rb', line 22

property :renewed_at,		DateTime

- (DateTime) renewed_at=(value)

Returns If a second notification for same what/whatId is logged, set this at that Date/Time

Returns:

  • (DateTime)

    If a second notification for same what/whatId is logged, set this at that Date/Time



22
# File 'models/notification.rb', line 22

property :renewed_at,		DateTime

- (Integer) renewed_count

Returns Number of times this Notification has been renewed

Returns:

  • (Integer)

    Number of times this Notification has been renewed



23
# File 'models/notification.rb', line 23

property :renewed_count,	Integer, :default => 0

- (Integer) renewed_count=(value)

Returns Number of times this Notification has been renewed

Returns:

  • (Integer)

    Number of times this Notification has been renewed



23
# File 'models/notification.rb', line 23

property :renewed_count,	Integer, :default => 0

- (Integer) uidToNotify

Returns ID of User this Notification applies to

Returns:

  • (Integer)

    ID of User this Notification applies to



15
# File 'models/notification.rb', line 15

property :uidToNotify,		Integer

- (Integer) uidToNotify=(value)

Returns ID of User this Notification applies to

Returns:

  • (Integer)

    ID of User this Notification applies to



15
# File 'models/notification.rb', line 15

property :uidToNotify,		Integer

- (Boolean) viewed

Returns True if this Notification has been viewed by User

Returns:

  • (Boolean)

    True if this Notification has been viewed by User



20
# File 'models/notification.rb', line 20

property :viewed,			Boolean, :default => false

- (Boolean) viewed=(value)

Returns True if this Notification has been viewed by User

Returns:

  • (Boolean)

    True if this Notification has been viewed by User



20
# File 'models/notification.rb', line 20

property :viewed,			Boolean, :default => false

- (DateTime) viewed_at

Returns Date/Time Notification was viewed by User

Returns:

  • (DateTime)

    Date/Time Notification was viewed by User



24
# File 'models/notification.rb', line 24

property :viewed_at,		DateTime

- (DateTime) viewed_at=(value)

Returns Date/Time Notification was viewed by User

Returns:

  • (DateTime)

    Date/Time Notification was viewed by User



24
# File 'models/notification.rb', line 24

property :viewed_at,		DateTime

- (LINK_TYPE) what

Returns Type of resource this Notification is attached to

Returns:

  • (LINK_TYPE)

    Type of resource this Notification is attached to



17
# File 'models/notification.rb', line 17

property :what,				Integer

- (LINK_TYPE) what=(value)

Returns Type of resource this Notification is attached to

Returns:

  • (LINK_TYPE)

    Type of resource this Notification is attached to



17
# File 'models/notification.rb', line 17

property :what,				Integer

- (Integer) whatId

Returns ID of resource this Notification is attached to

Returns:

  • (Integer)

    ID of resource this Notification is attached to



18
# File 'models/notification.rb', line 18

property :whatId,			Integer

- (Integer) whatId=(value)

Returns ID of resource this Notification is attached to

Returns:

  • (Integer)

    ID of resource this Notification is attached to



18
# File 'models/notification.rb', line 18

property :whatId,			Integer