Skip to content

Commit 5df6570

Browse files
authored
[stable17] Don't process known avatars from LDAP (#17372)
[stable17] Don't process known avatars from LDAP
2 parents 32df7f7 + 845e7f7 commit 5df6570

File tree

2 files changed

+113
-2
lines changed

2 files changed

+113
-2
lines changed

apps/user_ldap/lib/User/User.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,10 +584,26 @@ public function updateAvatar($force = false) {
584584
//not set, nothing left to do;
585585
return false;
586586
}
587+
587588
if(!$this->image->loadFromBase64(base64_encode($avatarImage))) {
588589
return false;
589590
}
590-
return $this->setOwnCloudAvatar();
591+
592+
// use the checksum before modifications
593+
$checksum = md5($this->image->data());
594+
595+
if($checksum === $this->config->getUserValue($this->uid, 'user_ldap', 'lastAvatarChecksum', '')) {
596+
return true;
597+
}
598+
599+
$isSet = $this->setOwnCloudAvatar();
600+
601+
if($isSet) {
602+
// save checksum only after successful setting
603+
$this->config->setUserValue($this->uid, 'user_ldap', 'lastAvatarChecksum', $checksum);
604+
}
605+
606+
return $isSet;
591607
}
592608

593609
/**
@@ -599,8 +615,10 @@ private function setOwnCloudAvatar() {
599615
$this->log->log('avatar image data from LDAP invalid for '.$this->dn, ILogger::ERROR);
600616
return false;
601617
}
618+
619+
602620
//make sure it is a square and not bigger than 128x128
603-
$size = min(array($this->image->width(), $this->image->height(), 128));
621+
$size = min([$this->image->width(), $this->image->height(), 128]);
604622
if(!$this->image->centerCrop($size)) {
605623
$this->log->log('croping image for avatar failed for '.$this->dn, ILogger::ERROR);
606624
return false;

apps/user_ldap/tests/User/UserTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,17 @@ public function testUpdateAvatarJpegPhotoProvided() {
521521
$this->image->expects($this->once())
522522
->method('centerCrop')
523523
->will($this->returnValue(true));
524+
$this->image->expects($this->once())
525+
->method('data')
526+
->will($this->returnValue('this is a photo'));
527+
528+
$this->config->expects($this->once())
529+
->method('getUserValue')
530+
->with($this->uid, 'user_ldap', 'lastAvatarChecksum', '')
531+
->willReturn('');
532+
$this->config->expects($this->once())
533+
->method('setUserValue')
534+
->with($this->uid, 'user_ldap', 'lastAvatarChecksum', md5('this is a photo'));
524535

525536
$this->filesystemhelper->expects($this->once())
526537
->method('isLoaded')
@@ -544,6 +555,53 @@ public function testUpdateAvatarJpegPhotoProvided() {
544555
$this->user->updateAvatar();
545556
}
546557

558+
public function testUpdateAvatarKnownJpegPhotoProvided() {
559+
$this->access->expects($this->once())
560+
->method('readAttribute')
561+
->with($this->equalTo($this->dn),
562+
$this->equalTo('jpegphoto'))
563+
->will($this->returnValue(['this is a photo']));
564+
565+
$this->image->expects($this->once())
566+
->method('loadFromBase64')
567+
->willReturn('imageResource');
568+
$this->image->expects($this->never())
569+
->method('valid');
570+
$this->image->expects($this->never())
571+
->method('width');
572+
$this->image->expects($this->never())
573+
->method('height');
574+
$this->image->expects($this->never())
575+
->method('centerCrop');
576+
$this->image->expects($this->once())
577+
->method('data')
578+
->will($this->returnValue('this is a photo'));
579+
580+
$this->config->expects($this->once())
581+
->method('getUserValue')
582+
->with($this->uid, 'user_ldap', 'lastAvatarChecksum', '')
583+
->willReturn(md5('this is a photo'));
584+
$this->config->expects($this->never())
585+
->method('setUserValue');
586+
587+
$this->filesystemhelper->expects($this->never())
588+
->method('isLoaded');
589+
590+
$avatar = $this->createMock(IAvatar::class);
591+
$avatar->expects($this->never())
592+
->method('set');
593+
594+
$this->avatarManager->expects($this->never())
595+
->method('getAvatar');
596+
597+
$this->connection->expects($this->any())
598+
->method('resolveRule')
599+
->with('avatar')
600+
->willReturn(['jpegphoto', 'thumbnailphoto']);
601+
602+
$this->assertTrue($this->user->updateAvatar());
603+
}
604+
547605
public function testUpdateAvatarThumbnailPhotoProvided() {
548606
$this->access->expects($this->any())
549607
->method('readAttribute')
@@ -575,6 +633,17 @@ public function testUpdateAvatarThumbnailPhotoProvided() {
575633
$this->image->expects($this->once())
576634
->method('centerCrop')
577635
->will($this->returnValue(true));
636+
$this->image->expects($this->once())
637+
->method('data')
638+
->will($this->returnValue('this is a photo'));
639+
640+
$this->config->expects($this->once())
641+
->method('getUserValue')
642+
->with($this->uid, 'user_ldap', 'lastAvatarChecksum', '')
643+
->willReturn('');
644+
$this->config->expects($this->once())
645+
->method('setUserValue')
646+
->with($this->uid, 'user_ldap', 'lastAvatarChecksum', md5('this is a photo'));
578647

579648
$this->filesystemhelper->expects($this->once())
580649
->method('isLoaded')
@@ -625,6 +694,13 @@ public function testUpdateAvatarCorruptPhotoProvided() {
625694
->method('height');
626695
$this->image->expects($this->never())
627696
->method('centerCrop');
697+
$this->image->expects($this->never())
698+
->method('data');
699+
700+
$this->config->expects($this->never())
701+
->method('getUserValue');
702+
$this->config->expects($this->never())
703+
->method('setUserValue');
628704

629705
$this->filesystemhelper->expects($this->never())
630706
->method('isLoaded');
@@ -675,6 +751,16 @@ public function testUpdateAvatarUnsupportedThumbnailPhotoProvided() {
675751
$this->image->expects($this->once())
676752
->method('centerCrop')
677753
->will($this->returnValue(true));
754+
$this->image->expects($this->once())
755+
->method('data')
756+
->will($this->returnValue('this is a photo'));
757+
758+
$this->config->expects($this->once())
759+
->method('getUserValue')
760+
->with($this->uid, 'user_ldap', 'lastAvatarChecksum', '')
761+
->willReturn('');
762+
$this->config->expects($this->never())
763+
->method('setUserValue');
678764

679765
$this->filesystemhelper->expects($this->once())
680766
->method('isLoaded')
@@ -723,6 +809,13 @@ public function testUpdateAvatarNotProvided() {
723809
->method('height');
724810
$this->image->expects($this->never())
725811
->method('centerCrop');
812+
$this->image->expects($this->never())
813+
->method('data');
814+
815+
$this->config->expects($this->never())
816+
->method('getUserValue');
817+
$this->config->expects($this->never())
818+
->method('setUserValue');
726819

727820
$this->filesystemhelper->expects($this->never())
728821
->method('isLoaded');

0 commit comments

Comments
 (0)