| 1 | package OpenInteract::SPOPS; | ||
| 2 | |||
| 3 | # $Id: SPOPS.pm,v 1.33 2003/04/01 03:53:38 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use Data::Dumper qw( Dumper ); | ||
| 7 | use Digest::MD5 qw( md5_hex ); | ||
| 8 | use OpenInteract::Utility; | ||
| 9 | |||
| 10 | @OpenInteract::SPOPS::ISA = (); | ||
| 11 | $OpenInteract::SPOPS::VERSION = sprintf("%d.%02d", q$Revision: 1.33 $ =~ /(\d+)\.(\d+)/); | ||
| 12 | |||
| 13 | use constant OBJECT_KEY_TABLE => 'object_keys'; | ||
| 14 | |||
| 15 | ######################################## | ||
| 16 | # RULESET | ||
| 17 | ######################################## | ||
| 18 | |||
| 19 | sub ruleset_factory { | ||
| 20 | my ( $class, $rs_table ) = @_; | ||
| 21 | push @{ $rs_table->{post_save_action} }, \&save_object_key; | ||
| 22 | return __PACKAGE__; | ||
| 23 | } | ||
| 24 | |||
| 25 | |||
| 26 | # Use the object class and ID to update the object key table | ||
| 27 | |||
| 28 | sub save_object_key { | ||
| 29 | my ( $self, $p ) = @_; | ||
| 30 | |||
| 31 | # Don't create an object key if we're explicitly told not to | ||
| 32 | return 1 if ( $self->CONFIG->{skip_object_key} || $p->{skip_object_key} ); | ||
| 33 | |||
| 34 | $p ||= {}; | ||
| 35 | my $obj_key = $self->fetch_object_key; | ||
| 36 | unless ( $obj_key ) { | ||
| 37 | $obj_key = $self->generate_object_key; | ||
| 38 | eval { $self->db_insert({ %{ $p }, table => OBJECT_KEY_TABLE, | ||
| 39 | field => [ qw/ object_key class object_id / ], | ||
| 40 | value => [ $obj_key, ref( $self ), scalar( $self->id ) ] }) }; | ||
| 41 | if ( $@ ) { | ||
| 42 | warn "Cannot save object key: $@\n"; | ||
| 43 | return undef; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | return $self->{tmp_object_key} = $obj_key; | ||
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | ######################################## | ||
| 51 | # DESCRIPTION | ||
| 52 | ######################################## | ||
| 53 | |||
| 54 | # Enhancement to the method in SPOPS | ||
| 55 | |||
| 56 | sub object_description { | ||
| 57 | my ( $self ) = @_; | ||
| 58 | my $desc = SPOPS::object_description( $self ); | ||
| 59 | my $date_field = $self->CONFIG->{description_date_field}; | ||
| 60 | if ( $date_field ) { | ||
| 61 | $desc->{date} = $self->{ $date_field }; | ||
| 62 | } | ||
| 63 | return $desc; | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | ######################################## | ||
| 68 | # OBJECT KEY | ||
| 69 | ######################################## | ||
| 70 | |||
| 71 | # Create a unique key based on the class and ID | ||
| 72 | |||
| 73 | sub generate_object_key { | ||
| 74 | my ( $self ) = @_; | ||
| 75 | return md5_hex( ref( $self ) . $self->id ); | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | # Retrieve the object key based on the class and ID | ||
| 80 | |||
| 81 | sub fetch_object_key { | ||
| 82 | my ( $self, $p ) = @_; | ||
| 83 | $p ||= {}; | ||
| 84 | my $row = $self->db_select({ %{ $p }, | ||
| 85 | from => OBJECT_KEY_TABLE, | ||
| 86 | select => [ 'object_key' ], | ||
| 87 | where => 'class = ? AND object_id = ?', | ||
| 88 | value => [ ref $self, scalar( $self->id ) ], | ||
| 89 | return => 'single' }); | ||
| 90 | return $row->[0] if ( $row ); | ||
| 91 | return undef; | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | # Retrieve the object class and ID given an object_key | ||
| 96 | |||
| 97 | sub fetch_object_info_by_key { | ||
| 98 | my ( $class, $key, $p ) = @_; | ||
| 99 | $p ||= {}; | ||
| 100 | $p->{db} ||= $class->global_datasource_handle; | ||
| 101 | die "Cannot retrieve object info without key!" unless ( $key ); | ||
| 102 | my $row = SPOPS::SQLInterface->db_select({ %{ $p }, | ||
| 103 | from => OBJECT_KEY_TABLE, | ||
| 104 | select => [ 'class', 'object_id' ], | ||
| 105 | where => 'object_key = ?', | ||
| 106 | value => [ $key ], | ||
| 107 | return => 'single' }); | ||
| 108 | return ( $row->[0], $row->[1] ) if ( $row ); | ||
| 109 | return undef; | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | # Retrieve an object given an object_key | ||
| 114 | |||
| 115 | sub fetch_object_by_key { | ||
| 116 | my ( $class, $key, $p ) = @_; | ||
| 117 | my ( $object_class, $object_id ) = $class->fetch_object_info_by_key( $key, $p ); | ||
| 118 | return $object_class->fetch( $object_id, $p ) if ( $object_class and $object_id ); | ||
| 119 | return undef; | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | ######################################## | ||
| 124 | # OBJECT TRACK METHODS | ||
| 125 | ######################################## | ||
| 126 | |||
| 127 | # Just a wrapper for log_action_enter, although we make sure that the | ||
| 128 | # action is allowed before doing it. | ||
| 129 | |||
| 130 | sub log_action { | ||
| 131 | my ( $self, $action, $id ) = @_; | ||
| 132 | if ( $action eq 'delete' ) { | ||
| 133 | return 1 unless ( $self->CONFIG->{track}{delete} || $self->CONFIG->{track}{remove} ); | ||
| 134 | $action = 'remove'; | ||
| 135 | } | ||
| 136 | else { | ||
| 137 | return 1 unless ( $self->CONFIG->{track}{ $action } ); | ||
| 138 | } | ||
| 139 | return $self->log_action_enter( $action, $id ); | ||
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | # Log the object, the action (create, update, remove), who did | ||
| 144 | # the action and when it was done. | ||
| 145 | # | ||
| 146 | # Note that you can pass the uid in directly to override the current user | ||
| 147 | |||
| 148 | sub log_action_enter { | ||
| 149 | my ( $self, $action, $id, $uid ) = @_; | ||
| 150 | my $R = OpenInteract::Request->instance; | ||
| 151 | $uid ||= ( ref $R->{auth}{user} ) ? $R->{auth}{user}{user_id} : 0; | ||
| 152 | my $now = SPOPS::Utility->now; | ||
| 153 | my $class = ref $self || $self; | ||
| 154 | my ( $log_msg ); | ||
| 155 | if ( my $apr = $R->apache ) { | ||
| 156 | $log_msg = $apr->param( '_log_message' ); | ||
| 157 | } | ||
| 158 | $R->DEBUG && $R->scrib( 1, "Log [$action] to [$class] [$id] by ", | ||
| 159 | "[$uid] on [$now]" ); | ||
| 160 | my $object_action = eval { $R->object_action->new({ | ||
| 161 | class => $class, | ||
| 162 | object_id => $id, | ||
| 163 | action => $action, | ||
| 164 | action_by => $uid, | ||
| 165 | action_on => $now, | ||
| 166 | notes => $log_msg }) | ||
| 167 | ->save() }; | ||
| 168 | if ( $@ ) { | ||
| 169 | $R->scrib( 0, "Log entry failed: $@" ); | ||
| 170 | OpenInteract::Error->set( SPOPS::Error->get ); | ||
| 171 | $OpenInteract::Error::user_msg = "Cannot log object action: $action"; | ||
| 172 | $OpenInteract::Error::notes = "Object: $class ($id) by $uid on $now"; | ||
| 173 | $R->throw({ code => 302 }); | ||
| 174 | return undef; | ||
| 175 | } | ||
| 176 | return 1; | ||
| 177 | } | ||
| 178 | |||
| 179 | |||
| 180 | # Retrieve the user who created a particular object | ||
| 181 | |||
| 182 | sub fetch_creator { | ||
| 183 | my ( $self ) = @_; | ||
| 184 | |||
| 185 | # Bail if it's not an already-existing object | ||
| 186 | return undef unless ( ref $self and $self->id ); | ||
| 187 | my $R = OpenInteract::Request->instance; | ||
| 188 | my $track = eval { $R->object_action->fetch_object_creation( $self ) }; | ||
| 189 | if ( $@ ) { | ||
| 190 | $OpenInteract::Error::user_msg = 'Cannot retrieve object creator(s)'; | ||
| 191 | $OpenInteract::Error::extra = { class => ref $self, | ||
| 192 | object_id => $self->id }; | ||
| 193 | $R->throw( { code => 306 } ); | ||
| 194 | return undef; | ||
| 195 | } | ||
| 196 | my $creator = eval { $track->action_by_user }; | ||
| 197 | if ( $@ ) { warn "Error fetching creator: $@" } | ||
| 198 | return $creator; | ||
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | # Return 1 if the user represented by $uid is a creator | ||
| 203 | # of an object (or the superuser), undef if not | ||
| 204 | |||
| 205 | sub is_creator { | ||
| 206 | my ( $self, $uid ) = @_; | ||
| 207 | my $R = OpenInteract::Request->instance; | ||
| 208 | $uid ||= $R->{auth}{user}{user_id}; | ||
| 209 | return undef unless ( $uid ); | ||
| 210 | |||
| 211 | # the great and powerful superuser sees all | ||
| 212 | |||
| 213 | return 1 if ( $uid eq $R->CONFIG->{default_objects}{superuser} ); | ||
| 214 | my $creator = $self->fetch_creator; | ||
| 215 | return ( $creator->id eq $uid ); | ||
| 216 | } | ||
| 217 | |||
| 218 | # TODO: Update this to just return the object... | ||
| 219 | # | ||
| 220 | # Retrieve an arrayref of arrayrefs where item 0 is the uid | ||
| 221 | # of the user who last did the update and item 1 is the | ||
| 222 | # date of the update | ||
| 223 | |||
| 224 | sub fetch_updates { | ||
| 225 | my ( $self, $opt ) = @_; | ||
| 226 | |||
| 227 | # Bail if it's not an already-saved object | ||
| 228 | |||
| 229 | return [] unless ( ref $self and $self->id ); | ||
| 230 | my $limit = ( $opt eq 'last' ) ? '1' : int( $opt ); | ||
| 231 | my $R = OpenInteract::Request->instance; | ||
| 232 | my $updates = eval { $R->object_action->fetch_actions( | ||
| 233 | $self, { limit => $limit, | ||
| 234 | column_group => 'base' } ) }; | ||
| 235 | if ( $@ ) { | ||
| 236 | $OpenInteract::Error::user_msg = 'Cannot retrieve object updates'; | ||
| 237 | $OpenInteract::Error::extra = { class => ref $self, | ||
| 238 | object_id => $self->id }; | ||
| 239 | $R->throw( { code => 306 } ); | ||
| 240 | return undef; | ||
| 241 | } | ||
| 242 | $R->DEBUG && $R->scrib( 2, "Data from updates:\n", Dumper( $updates ) ); | ||
| 243 | return [ map { [ $_->{action_by}, $_->{action_on} ] } @{ $updates } ]; | ||
| 244 | } | ||
| 245 | |||
| 246 | |||
| 247 | ######################################## | ||
| 248 | # SECURITY | ||
| 249 | ######################################## | ||
| 250 | |||
| 251 | # Override method in SPOPS::Secure since we already know the | ||
| 252 | # user/group information from $R | ||
| 253 | |||
| 254 | sub get_security_scopes { | ||
| 255 | my ( $self, $p ) = @_; | ||
| 256 | my $R = OpenInteract::Request->instance; | ||
| 257 | return ( $R->{auth}{user}, $R->{auth}{group} ); | ||
| 258 | } | ||
| 259 | |||
| 260 | |||
| 261 | |||
| 262 | # Let SPOPS::Secure know what the IDs are for the superuser and | ||
| 263 | # supergroup | ||
| 264 | |||
| 265 | sub get_superuser_id { return $_[0]->global_config->{default_objects}{superuser} } | ||
| 266 | sub get_supergroup_id { return $_[0]->global_config->{default_objects}{supergroup} } | ||
| 267 | |||
| 268 | |||
| 269 | ######################################## | ||
| 270 | # GLOBAL OBJECTS/CLASSES | ||
| 271 | ######################################## | ||
| 272 | |||
| 273 | # These are used so that subclasses (and other classes in the | ||
| 274 | # inheritance hierarchy, particularly within SPOPS) are able to have | ||
| 275 | # access to the various objects and resources | ||
| 276 | |||
| 277 | sub global_cache { | ||
| 278 | my ( $self, @params ) = @_; | ||
| 279 | my $R = OpenInteract::Request->instance; | ||
| 280 | if ( $R->CONFIG->{cache_info}{data}{use_spops} ) { | ||
| 281 | return $R->cache; | ||
| 282 | } | ||
| 283 | return undef; | ||
| 284 | } | ||
| 285 | |||
| 286 | sub global_config { return OpenInteract::Request->instance->config } | ||
| 287 | sub global_secure_class { return OpenInteract::Request->instance->secure } | ||
| 288 | sub global_security_object_class { return OpenInteract::Request->instance->security } | ||
| 289 | sub global_user_class { return OpenInteract::Request->user } | ||
| 290 | sub global_group_class { return OpenInteract::Request->group } | ||
| 291 | sub global_user_current { return OpenInteract::Request->instance->{auth}{user} } | ||
| 292 | sub global_group_current { return OpenInteract::Request->instance->{auth}{group} } | ||
| 293 | |||
| 294 | |||
| 295 | ######################################## | ||
| 296 | # OTHER METHODS | ||
| 297 | ######################################## | ||
| 298 | |||
| 299 | |||
| 300 | # Send an email with one or more objects as the body. | ||
| 301 | |||
| 302 | sub notify { | ||
| 303 | my ( $item, $p ) = @_; | ||
| 304 | my $R = OpenInteract::Request->instance; | ||
| 305 | $p->{object} ||= []; | ||
| 306 | |||
| 307 | # If we weren't given any objects and we were called by | ||
| 308 | # a class instead of an object | ||
| 309 | |||
| 310 | return undef unless ( ref $item or scalar @{ $p->{object} } ); | ||
| 311 | |||
| 312 | # If we were just called by an object, make it our message | ||
| 313 | |||
| 314 | push @{ $p->{object} }, $item unless ( scalar @{ $p->{object} } ); | ||
| 315 | my $num_objects = scalar @{ $p->{object} }; | ||
| 316 | my $subject = $p->{subject} || "Object notification: $num_objects objects in mail"; | ||
| 317 | my $separator = '=' x 25; | ||
| 318 | my $msg = ( $p->{notes} ) ? | ||
| 319 | join( "\n", 'Notes', "$separator$p->{notes}", $separator, "\n" ) : ''; | ||
| 320 | foreach my $obj ( @{ $p->{object} } ) { | ||
| 321 | my $info = $obj->object_description; | ||
| 322 | my $object_url = join( '', 'http://', $R->{server_name}, $info->{url} ); | ||
| 323 | $msg .= <<OBJECT; | ||
| 324 | Begin $info->{name} object | ||
| 325 | $separator | ||
| 326 | @{[ $obj->as_string ]} | ||
| 327 | |||
| 328 | View this object at: $object_url | ||
| 329 | $separator | ||
| 330 | End $p->{name} object | ||
| 331 | |||
| 332 | OBJECT | ||
| 333 | } | ||
| 334 | my $from_email = $p->{email_from} || | ||
| 335 | $R->CONFIG->{mail}{admin_email} || | ||
| 336 | $R->CONFIG->{admin_email}; | ||
| 337 | eval { | ||
| 338 | OpenInteract::Utility->send_email({ to => $p->{email}, | ||
| 339 | from => $from_email, | ||
| 340 | subject => $subject, | ||
| 341 | message => $msg }); | ||
| 342 | }; | ||
| 343 | if ( $@ ) { | ||
| 344 | $R->throw({ code => 203 }); | ||
| 345 | return undef; | ||
| 346 | } | ||
| 347 | return 1; | ||
| 348 | } | ||
| 349 | |||
| 350 | |||
| 351 | 1; | ||
| 352 | |||
| 353 | __END__ |