liquid_feedback_frontend

annotate model/member.lua @ 286:c587d8762e62

Registration process updated for Core 2.0, lockable member fields, notification settings
author bsw
date Sat Feb 25 11:51:37 2012 +0100 (2012-02-25)
parents b77e6a17ca77
children c4dd225578d4
rev   line source
bsw/jbe@0 1 Member = mondelefant.new_class()
bsw/jbe@0 2 Member.table = 'member'
bsw/jbe@0 3
bsw/jbe@0 4 Member:add_reference{
bsw@9 5 mode = "1m",
bsw@9 6 to = "MemberHistory",
bsw@9 7 this_key = 'id',
bsw@9 8 that_key = 'member_id',
bsw@9 9 ref = 'history_entries',
bsw@9 10 back_ref = 'member'
bsw@9 11 }
bsw@9 12
bsw@9 13 Member:add_reference{
bsw/jbe@4 14 mode = '1m',
bsw@2 15 to = "MemberImage",
bsw@2 16 this_key = 'id',
bsw@2 17 that_key = 'member_id',
bsw/jbe@4 18 ref = 'images',
bsw@2 19 back_ref = 'member'
bsw@2 20 }
bsw@2 21
bsw@2 22 Member:add_reference{
bsw/jbe@0 23 mode = '1m',
bsw/jbe@0 24 to = "Contact",
bsw/jbe@0 25 this_key = 'id',
bsw/jbe@0 26 that_key = 'member_id',
bsw/jbe@0 27 ref = 'contacts',
bsw/jbe@0 28 back_ref = 'member',
bsw/jbe@0 29 default_order = '"other_member_id"'
bsw/jbe@0 30 }
bsw/jbe@0 31
bsw/jbe@0 32 Member:add_reference{
bsw/jbe@0 33 mode = '1m',
bsw/jbe@0 34 to = "Contact",
bsw/jbe@0 35 this_key = 'id',
bsw/jbe@0 36 that_key = 'member_id',
bsw/jbe@0 37 ref = 'foreign_contacts',
bsw/jbe@0 38 back_ref = 'other_member',
bsw/jbe@0 39 default_order = '"member_id"'
bsw/jbe@0 40 }
bsw/jbe@0 41
bsw/jbe@0 42 Member:add_reference{
bsw/jbe@0 43 mode = '1m',
bsw/jbe@0 44 to = "Session",
bsw/jbe@0 45 this_key = 'id',
bsw/jbe@0 46 that_key = 'member_id',
bsw/jbe@0 47 ref = 'sessions',
bsw/jbe@0 48 back_ref = 'member',
bsw/jbe@0 49 default_order = '"ident"'
bsw/jbe@0 50 }
bsw/jbe@0 51
bsw/jbe@0 52 Member:add_reference{
bsw/jbe@0 53 mode = '1m',
bsw/jbe@0 54 to = "Draft",
bsw/jbe@0 55 this_key = 'id',
bsw/jbe@0 56 that_key = 'author_id',
bsw/jbe@0 57 ref = 'drafts',
bsw/jbe@0 58 back_ref = 'author',
bsw/jbe@0 59 default_order = '"id"'
bsw/jbe@0 60 }
bsw/jbe@0 61
bsw/jbe@0 62 Member:add_reference{
bsw/jbe@0 63 mode = '1m',
bsw/jbe@0 64 to = "Suggestion",
bsw/jbe@0 65 this_key = 'id',
bsw/jbe@0 66 that_key = 'author_id',
bsw/jbe@0 67 ref = 'suggestions',
bsw/jbe@0 68 back_ref = 'author',
bsw/jbe@0 69 default_order = '"id"'
bsw/jbe@0 70 }
bsw/jbe@0 71
bsw/jbe@0 72 Member:add_reference{
bsw/jbe@0 73 mode = '1m',
bsw/jbe@0 74 to = "Membership",
bsw/jbe@0 75 this_key = 'id',
bsw/jbe@0 76 that_key = 'member_id',
bsw/jbe@0 77 ref = 'memberships',
bsw/jbe@0 78 back_ref = 'member',
bsw/jbe@0 79 default_order = '"area_id"'
bsw/jbe@0 80 }
bsw/jbe@0 81
bsw/jbe@0 82 Member:add_reference{
bsw/jbe@0 83 mode = '1m',
bsw/jbe@0 84 to = "Interest",
bsw/jbe@0 85 this_key = 'id',
bsw/jbe@0 86 that_key = 'member_id',
bsw/jbe@0 87 ref = 'interests',
bsw/jbe@0 88 back_ref = 'member',
bsw/jbe@0 89 default_order = '"id"'
bsw/jbe@0 90 }
bsw/jbe@0 91
bsw/jbe@0 92 Member:add_reference{
bsw/jbe@0 93 mode = '1m',
bsw/jbe@0 94 to = "Initiator",
bsw/jbe@0 95 this_key = 'id',
bsw/jbe@0 96 that_key = 'member_id',
bsw/jbe@0 97 ref = 'initiators',
bsw@10 98 back_ref = 'member'
bsw/jbe@0 99 }
bsw/jbe@0 100
bsw/jbe@0 101 Member:add_reference{
bsw/jbe@0 102 mode = '1m',
bsw/jbe@0 103 to = "Supporter",
bsw/jbe@0 104 this_key = 'id',
bsw/jbe@0 105 that_key = 'member_id',
bsw/jbe@0 106 ref = 'supporters',
bsw@2 107 back_ref = 'member'
bsw/jbe@0 108 }
bsw/jbe@0 109
bsw/jbe@0 110 Member:add_reference{
bsw/jbe@0 111 mode = '1m',
bsw/jbe@0 112 to = "Opinion",
bsw/jbe@0 113 this_key = 'id',
bsw/jbe@0 114 that_key = 'member_id',
bsw/jbe@0 115 ref = 'opinions',
bsw/jbe@0 116 back_ref = 'member',
bsw/jbe@0 117 default_order = '"id"'
bsw/jbe@0 118 }
bsw/jbe@0 119
bsw/jbe@0 120 Member:add_reference{
bsw/jbe@0 121 mode = '1m',
bsw/jbe@0 122 to = "Delegation",
bsw/jbe@0 123 this_key = 'id',
bsw/jbe@0 124 that_key = 'truster_id',
bsw/jbe@0 125 ref = 'outgoing_delegations',
bsw/jbe@0 126 back_ref = 'truster',
bsw/jbe@0 127 default_order = '"id"'
bsw/jbe@0 128 }
bsw/jbe@0 129
bsw/jbe@0 130 Member:add_reference{
bsw/jbe@0 131 mode = '1m',
bsw/jbe@0 132 to = "Delegation",
bsw/jbe@0 133 this_key = 'id',
bsw/jbe@0 134 that_key = 'trustee_id',
bsw/jbe@0 135 ref = 'incoming_delegations',
bsw/jbe@0 136 back_ref = 'trustee',
bsw/jbe@0 137 default_order = '"id"'
bsw/jbe@0 138 }
bsw/jbe@0 139
bsw/jbe@0 140 Member:add_reference{
bsw/jbe@0 141 mode = '1m',
bsw/jbe@0 142 to = "DirectVoter",
bsw/jbe@0 143 this_key = 'id',
bsw/jbe@0 144 that_key = 'member_id',
bsw/jbe@0 145 ref = 'direct_voter',
bsw/jbe@0 146 back_ref = 'member',
bsw/jbe@0 147 default_order = '"issue_id"'
bsw/jbe@0 148 }
bsw/jbe@0 149
bsw/jbe@0 150 Member:add_reference{
bsw/jbe@0 151 mode = '1m',
bsw/jbe@0 152 to = "Vote",
bsw/jbe@0 153 this_key = 'id',
bsw/jbe@0 154 that_key = 'member_id',
bsw/jbe@0 155 ref = 'vote',
bsw/jbe@0 156 back_ref = 'member',
bsw/jbe@0 157 default_order = '"issue_id", "initiative_id"'
bsw/jbe@0 158 }
bsw/jbe@0 159
bsw/jbe@0 160 Member:add_reference{
bsw/jbe@0 161 mode = 'mm',
bsw/jbe@0 162 to = "Member",
bsw/jbe@0 163 this_key = 'id',
bsw/jbe@0 164 that_key = 'id',
bsw/jbe@0 165 connected_by_table = 'contact',
bsw/jbe@0 166 connected_by_this_key = 'member_id',
bsw/jbe@0 167 connected_by_that_key = 'other_member_id',
bsw/jbe@0 168 ref = 'saved_members',
bsw/jbe@0 169 }
bsw/jbe@0 170
bsw/jbe@0 171 Member:add_reference{
bsw/jbe@0 172 mode = 'mm',
bsw/jbe@0 173 to = "Member",
bsw/jbe@0 174 this_key = 'id',
bsw/jbe@0 175 that_key = 'id',
bsw/jbe@0 176 connected_by_table = 'contact',
bsw/jbe@0 177 connected_by_this_key = 'other_member_id',
bsw/jbe@0 178 connected_by_that_key = 'member_id',
bsw/jbe@0 179 ref = 'saved_by_members',
bsw/jbe@0 180 }
bsw/jbe@0 181
bsw/jbe@0 182 Member:add_reference{
bsw/jbe@0 183 mode = 'mm',
bsw@281 184 to = "Unit",
bsw@281 185 this_key = 'id',
bsw@281 186 that_key = 'id',
bsw@281 187 connected_by_table = 'privilege',
bsw@281 188 connected_by_this_key = 'member_id',
bsw@281 189 connected_by_that_key = 'unit_id',
bsw@281 190 ref = 'units'
bsw@281 191 }
bsw@281 192
bsw@281 193 Member:add_reference{
bsw@281 194 mode = 'mm',
bsw/jbe@0 195 to = "Area",
bsw/jbe@0 196 this_key = 'id',
bsw/jbe@0 197 that_key = 'id',
bsw/jbe@0 198 connected_by_table = 'membership',
bsw/jbe@0 199 connected_by_this_key = 'member_id',
bsw/jbe@0 200 connected_by_that_key = 'area_id',
bsw/jbe@0 201 ref = 'areas'
bsw/jbe@0 202 }
bsw/jbe@0 203
bsw/jbe@0 204 Member:add_reference{
bsw/jbe@0 205 mode = 'mm',
bsw/jbe@0 206 to = "Issue",
bsw/jbe@0 207 this_key = 'id',
bsw/jbe@0 208 that_key = 'id',
bsw/jbe@0 209 connected_by_table = 'interest',
bsw/jbe@0 210 connected_by_this_key = 'member_id',
bsw/jbe@0 211 connected_by_that_key = 'issue_id',
bsw/jbe@0 212 ref = 'issues'
bsw/jbe@0 213 }
bsw/jbe@0 214
bsw/jbe@0 215 Member:add_reference{
bsw/jbe@0 216 mode = 'mm',
bsw/jbe@0 217 to = "Initiative",
bsw/jbe@0 218 this_key = 'id',
bsw/jbe@0 219 that_key = 'id',
bsw/jbe@0 220 connected_by_table = 'initiator',
bsw/jbe@0 221 connected_by_this_key = 'member_id',
bsw/jbe@0 222 connected_by_that_key = 'initiative_id',
bsw/jbe@0 223 ref = 'initiated_initiatives'
bsw/jbe@0 224 }
bsw/jbe@0 225
bsw/jbe@0 226 Member:add_reference{
bsw/jbe@0 227 mode = 'mm',
bsw/jbe@0 228 to = "Initiative",
bsw/jbe@0 229 this_key = 'id',
bsw/jbe@0 230 that_key = 'id',
bsw/jbe@0 231 connected_by_table = 'supporter',
bsw/jbe@0 232 connected_by_this_key = 'member_id',
bsw/jbe@0 233 connected_by_that_key = 'initiative_id',
bsw/jbe@0 234 ref = 'supported_initiatives'
bsw/jbe@0 235 }
bsw/jbe@0 236
bsw@279 237 model.has_rendered_content(Member, RenderedMemberStatement, "statement")
bsw@279 238
bsw@199 239 Member:new_selector()
bsw@199 240 :add_order_by("member.name")
bsw@199 241 :exec()
bsw@199 242
bsw@193 243 function Member:build_selector(args)
bsw@193 244 local selector = self:new_selector()
bsw@193 245 if args.active ~= nil then
bsw@193 246 selector:add_where{ "member.active = ?", args.active }
bsw@193 247 end
bsw@199 248 if args.is_contact_of_member_id then
bsw@199 249 selector:join("contact", "__model_member__contact", "member.id = __model_member__contact.other_member_id")
bsw@199 250 selector:add_where{ "__model_member__contact.member_id = ?", args.is_contact_of_member_id }
bsw@199 251 end
bsw@193 252 if args.order then
bsw@193 253 if args.order == "id" then
bsw@193 254 selector:add_order_by("id")
bsw@193 255 elseif args.order == "login" then
bsw@193 256 selector:add_order_by("login")
bsw@193 257 elseif args.order == "name" then
bsw@193 258 selector:add_order_by("name")
bsw@193 259 else
bsw@193 260 error("invalid order")
bsw@193 261 end
bsw@193 262 end
bsw@193 263 return selector
bsw@193 264 end
bsw@193 265
bsw/jbe@0 266 function Member.object:set_password(password)
bsw/jbe@0 267 local hash = os.crypt(
bsw/jbe@0 268 password,
bsw/jbe@0 269 "$1$" .. multirand.string(
bsw/jbe@0 270 8,
bsw/jbe@0 271 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./"
bsw/jbe@0 272 )
bsw/jbe@0 273 )
bsw/jbe@0 274 assert(hash, "os.crypt failed")
bsw/jbe@0 275 self.password = hash
bsw/jbe@0 276 end
bsw/jbe@0 277
bsw/jbe@0 278 function Member.object:check_password(password)
bsw/jbe@0 279 if type(password) == "string" and type(self.password) == "string" then
bsw/jbe@0 280 return os.crypt(password, self.password) == self.password
bsw/jbe@0 281 else
bsw/jbe@0 282 return false
bsw/jbe@0 283 end
bsw/jbe@0 284 end
bsw/jbe@0 285
bsw/jbe@0 286 function Member.object_get:published_contacts()
bsw/jbe@0 287 return Member:new_selector()
bsw/jbe@0 288 :join('"contact"', nil, '"contact"."other_member_id" = "member"."id"')
bsw/jbe@0 289 :add_where{ '"contact"."member_id" = ?', self.id }
bsw/jbe@0 290 :add_where("public")
bsw/jbe@0 291 :exec()
bsw/jbe@0 292 end
bsw/jbe@0 293
bsw/jbe@0 294 function Member:by_login_and_password(login, password)
bsw/jbe@0 295 local selector = self:new_selector()
bsw/jbe@5 296 selector:add_where{'"login" = ?', login }
bsw@203 297 selector:add_where('NOT "locked"')
bsw/jbe@0 298 selector:optional_object_mode()
bsw/jbe@0 299 local member = selector:exec()
bsw/jbe@0 300 if member and member:check_password(password) then
bsw/jbe@0 301 return member
bsw/jbe@0 302 else
bsw/jbe@0 303 return nil
bsw/jbe@0 304 end
bsw/jbe@0 305 end
bsw/jbe@0 306
bsw/jbe@5 307 function Member:by_login(login)
bsw/jbe@5 308 local selector = self:new_selector()
bsw/jbe@5 309 selector:add_where{'"login" = ?', login }
bsw/jbe@5 310 selector:optional_object_mode()
bsw/jbe@5 311 return selector:exec()
bsw/jbe@5 312 end
bsw/jbe@5 313
bsw/jbe@5 314 function Member:by_name(name)
bsw/jbe@5 315 local selector = self:new_selector()
bsw/jbe@5 316 selector:add_where{'"name" = ?', name }
bsw/jbe@5 317 selector:optional_object_mode()
bsw/jbe@5 318 return selector:exec()
bsw/jbe@5 319 end
bsw/jbe@5 320
bsw@2 321 function Member:get_search_selector(search_string)
bsw/jbe@0 322 return self:new_selector()
bsw@2 323 :add_field( {'"highlight"("member"."name", ?)', search_string }, "name_highlighted")
bsw@2 324 :add_where{ '"member"."text_search_data" @@ "text_search_query"(?)', search_string }
bsw/jbe@0 325 :add_where("active")
bsw/jbe@0 326 end
bsw@2 327
bsw@286 328 function Member.object:send_invitation()
bsw@286 329 trace.disable()
bsw@286 330 self.invite_code = multirand.string( 24, "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" )
bsw@286 331 local content = slot.use_temporary(function()
bsw@286 332 slot.put(_"Hello\n\n")
bsw@286 333 slot.put(_"You are invited to LiquidFeedback. To register please click the following link:\n\n")
bsw@286 334 slot.put(config.absolute_base_url .. "index/register.html?invite_key=" .. self.invite_code .. "\n\n")
bsw@286 335 slot.put(_"If this link is not working, please open following url in your web browser:\n\n")
bsw@286 336 slot.put(config.absolute_base_url .. "index/register.html\n\n")
bsw@286 337 slot.put(_"On that page please enter the invite key:\n\n")
bsw@286 338 slot.put(self.invite_code .. "\n\n")
bsw@286 339 end)
bsw@286 340 local success = net.send_mail{
bsw@286 341 envelope_from = config.mail_envelope_from,
bsw@286 342 from = config.mail_from,
bsw@286 343 reply_to = config.mail_reply_to,
bsw@286 344 to = self.notify_email_unconfirmed or self.notify_email,
bsw@286 345 subject = config.mail_subject_prefix .. _"Invitation to LiquidFeedback",
bsw@286 346 content_type = "text/plain; charset=UTF-8",
bsw@286 347 content = content
bsw@286 348 }
bsw@286 349 return success
bsw@286 350 end
bsw@286 351
bsw/jbe@6 352 function Member.object:set_notify_email(notify_email)
bsw@224 353 trace.disable()
bsw/jbe@6 354 local expiry = db:query("SELECT now() + '7 days'::interval as expiry", "object").expiry
bsw/jbe@6 355 self.notify_email_unconfirmed = notify_email
bsw/jbe@6 356 self.notify_email_secret = multirand.string( 24, "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" )
bsw/jbe@6 357 self.notify_email_secret_expiry = expiry
bsw/jbe@6 358 local content = slot.use_temporary(function()
bsw/jbe@6 359 slot.put(_"Hello " .. self.name .. ",\n\n")
bsw/jbe@6 360 slot.put(_"Please confirm your email address by clicking the following link:\n\n")
bsw/jbe@6 361 slot.put(config.absolute_base_url .. "index/confirm_notify_email.html?secret=" .. self.notify_email_secret .. "\n\n")
bsw/jbe@6 362 slot.put(_"If this link is not working, please open following url in your web browser:\n\n")
bsw/jbe@6 363 slot.put(config.absolute_base_url .. "index/confirm_notify_email.html\n\n")
bsw/jbe@6 364 slot.put(_"On that page please enter the confirmation code:\n\n")
bsw/jbe@6 365 slot.put(self.notify_email_secret .. "\n\n")
bsw/jbe@6 366 end)
bsw/jbe@6 367 local success = net.send_mail{
bsw/jbe@6 368 envelope_from = config.mail_envelope_from,
bsw/jbe@6 369 from = config.mail_from,
bsw/jbe@6 370 reply_to = config.mail_reply_to,
bsw/jbe@6 371 to = self.notify_email_unconfirmed,
bsw/jbe@6 372 subject = config.mail_subject_prefix .. _"Email confirmation request",
bsw/jbe@6 373 content_type = "text/plain; charset=UTF-8",
bsw/jbe@6 374 content = content
bsw/jbe@6 375 }
bsw@75 376 if success then
bsw@75 377 local lock_expiry = db:query("SELECT now() + '1 hour'::interval AS lock_expiry", "object").lock_expiry
bsw@75 378 self.notify_email_lock_expiry = lock_expiry
bsw@75 379 end
bsw@75 380 self:save()
bsw/jbe@6 381 return success
bsw/jbe@6 382 end
bsw@11 383
bsw/jbe@19 384 function Member.object:get_setting(key)
bsw@79 385 return Setting:by_pk(self.id, key)
bsw/jbe@19 386 end
bsw/jbe@19 387
bsw/jbe@19 388 function Member.object:get_setting_value(key)
bsw@79 389 local setting = Setting:by_pk(self.id, key)
bsw/jbe@19 390 if setting then
bsw/jbe@19 391 return setting.value
bsw/jbe@19 392 end
bsw@11 393 end
bsw@11 394
bsw@11 395 function Member.object:set_setting(key, value)
bsw/jbe@19 396 local setting = self:get_setting(key)
bsw/jbe@19 397 if not setting then
bsw/jbe@19 398 setting = Setting:new()
bsw@79 399 setting.member_id = self.id
bsw/jbe@19 400 setting.key = key
bsw/jbe@19 401 end
bsw/jbe@19 402 setting.value = value
bsw/jbe@19 403 setting:save()
bsw@11 404 end
bsw@11 405
bsw@11 406 function Member.object:get_setting_maps_by_key(key)
bsw@11 407 return SettingMap:new_selector()
bsw@11 408 :add_where{ "member_id = ?", self.id }
bsw@11 409 :add_where{ "key = ?", key }
bsw@11 410 :add_order_by("subkey")
bsw@11 411 :exec()
bsw@11 412 end
bsw@11 413
bsw@11 414 function Member.object:get_setting_map_by_key_and_subkey(key, subkey)
bsw@11 415 return SettingMap:new_selector()
bsw@11 416 :add_where{ "member_id = ?", self.id }
bsw@11 417 :add_where{ "key = ?", key }
bsw@11 418 :add_where{ "subkey = ?", subkey }
bsw@11 419 :add_order_by("subkey")
bsw@11 420 :optional_object_mode()
bsw@11 421 :exec()
bsw@11 422 end
bsw@11 423
bsw@11 424 function Member.object:set_setting_map(key, subkey, value)
poelzi@144 425 setting_map = self:get_setting_map_by_key_and_subkey(key, subkey)
poelzi@144 426 if not setting_map then
poelzi@144 427 setting_map = SettingMap:new()
poelzi@144 428 setting_map.member_id = self.id
poelzi@144 429 setting_map.key = key
poelzi@144 430 setting_map.subkey = subkey
poelzi@144 431 end
poelzi@144 432 setting_map.value = value
poelzi@144 433 setting_map:save()
bsw@11 434 end
bsw@75 435
bsw@75 436 function Member.object_get:notify_email_locked()
bsw@75 437 return(
bsw@75 438 Member:new_selector()
bsw@75 439 :add_where{ "id = ?", app.session.member.id }
bsw@75 440 :add_where("notify_email_lock_expiry > now()")
bsw@75 441 :count() == 1
bsw@75 442 )
poelzi@134 443 end
poelzi@134 444
bsw@273 445 function Member.object_get:units_with_voting_right()
bsw@273 446 return(Unit:new_selector()
bsw@273 447 :join("privilege", nil, { "privilege.unit_id = unit.id AND privilege.member_id = ? AND privilege.voting_right", self.id })
bsw@273 448 :exec()
bsw@273 449 )
bsw@273 450 end
bsw@273 451
poelzi@134 452 function Member.object:ui_field_text(args)
poelzi@134 453 args = args or {}
poelzi@134 454 if app.session.member_id or config.public_access == "pseudonym" then
poelzi@134 455 -- ugly workaround for getting html into a replaced string and to the user
poelzi@134 456 ui.container{label = args.label, label_attr={class="ui_field_label"}, content = function()
poelzi@134 457 slot.put(string.format('<span><a href="%s">%s</a></span>',
poelzi@134 458 encode.url{
poelzi@134 459 module = "member",
poelzi@134 460 view = "show",
poelzi@134 461 id = self.id,
poelzi@134 462 },
poelzi@134 463 encode.html(self.name)))
poelzi@134 464 end
poelzi@134 465 }
poelzi@134 466 else
poelzi@134 467 ui.field.text{ label = args.label, value = _"[not displayed public]" }
poelzi@134 468 end
poelzi@134 469 end
bsw@281 470
bsw@281 471 function Member.object:has_voting_right_for_unit_id(unit_id)
bsw@281 472 return (Privilege:new_selector()
bsw@281 473 :add_where{ "member_id = ?", self.id }
bsw@281 474 :add_where{ "unit_id = ?", unit_id }
bsw@281 475 :add_where("voting_right")
bsw@281 476 :optional_object_mode()
bsw@281 477 :for_share()
bsw@281 478 :exec()) and true or false
bsw@281 479 end

Impressum / About Us