Repository: oppia/oppia

Issue:
[BUG]: Blog Admin must be able to review the blog editors in the /blog-admin page, and internal server errors if blog admin assignes a user again with same role again
### Describe the bug

The blog admin must be able to see/review the lists of blog editors in the /blog-admin page, also can revoke the blog editor role from that list itself. Also there are some internal server errors in the /blog-admin page when a blog admin ->
1) assignes a user with the same role again
2) assignes a role to a user that donot exists.

### Steps To Reproduce

1) Blog Admin goes to /blog-admin page 
2) assign a user with blog editor role
3) again assign that user with the blog editor role
4) assign a role to user that donot exist

### Expected Behavior

1) blog admin must be able to review and remove the blog editors from the /blog-admin page.
2) there must be no server errors, instead a warning/popup must be there.

### Screenshots/Videos

<img width="1426" alt="Screenshot 2023-01-27 at 1 44 15 PM" src="https://user-images.githubusercontent.com/91013793/215041422-a0350b83-e5e2-4d7a-beaa-994b3a82511a.png">


### What device are you using?

_No response_

### Operating System

_No response_

### What browsers are you seeing the problem on?

_No response_

### Browser version

_No response_

### Additional context

_No response_
[BUG]: Blog Admin must be able to review the blog editors in the /blog-admin page, and internal server errors if blog admin assignes a user again with same role again
### Describe the bug

The blog admin must be able to see/review the lists of blog editors in the /blog-admin page, also can revoke the blog editor role from that list itself. Also there are some internal server errors in the /blog-admin page when a blog admin ->
1) assignes a user with the same role again
2) assignes a role to a user that donot exists.

### Steps To Reproduce

1) Blog Admin goes to /blog-admin page 
2) assign a user with blog editor role
3) again assign that user with the blog editor role
4) assign a role to user that donot exist

### Expected Behavior

1) blog admin must be able to review and remove the blog editors from the /blog-admin page.
2) there must be no server errors, instead a warning/popup must be there.

### Screenshots/Videos

<img width="1426" alt="Screenshot 2023-01-27 at 1 44 15 PM" src="https://user-images.githubusercontent.com/91013793/215041422-a0350b83-e5e2-4d7a-beaa-994b3a82511a.png">


### What device are you using?

_No response_

### Operating System

_No response_

### What browsers are you seeing the problem on?

_No response_

### Browser version

_No response_

### Additional context

_No response_


Discussion:
@Shivkant-Chauhan Will be working on this issue only after all bugs for the blog release are addressed!
// cc @seanlip 
Hi @Rijuta-s 
Could you please provide me with an update when you plan to open the PR?
thanks!
@Shivkant-Chauhan Will be working on this issue only after all bugs for the blog release are addressed!
// cc @seanlip 
Hi @Rijuta-s 
Could you please provide me with an update when you plan to open the PR?
thanks!

Resolving patch:
diff --git a/core/domain/user_services.py b/core/domain/user_services.py
--- a/core/domain/user_services.py
+++ b/core/domain/user_services.py
@@ -1580,6 +1580,8 @@ def add_user_role(user_id: str, role: str) -> None:
         raise Exception('The role of a Mobile Learner cannot be changed.')
     if role in feconf.ALLOWED_DEFAULT_USER_ROLES_ON_REGISTRATION:
         raise Exception('Adding a %s role is not allowed.' % role)
+    if role in user_settings.roles:
+        raise Exception('The user already has this role.')
     user_settings.roles.append(role)
     role_services.log_role_query(
         user_id, feconf.ROLE_ACTION_ADD, role=role,


Test patch:
diff --git a/core/domain/user_services_test.py b/core/domain/user_services_test.py
--- a/core/domain/user_services_test.py
+++ b/core/domain/user_services_test.py
@@ -1302,6 +1302,26 @@ def test_add_user_role_to_mobile_learner_raises_exception(self) -> None:
             user_services.add_user_role(
                 profile_user_id, feconf.ROLE_ID_FULL_USER)
 
+    def test_add_duplicate_user_role_raises_exception(self) -> None:
+        auth_id = 'test_id'
+        user_email = 'test@gmail.com'
+
+        user_id = user_services.create_new_user(auth_id, user_email).user_id
+        self.assertEqual(
+            user_services.get_user_roles_from_id(user_id),
+            [feconf.ROLE_ID_FULL_USER])
+
+        user_services.add_user_role(
+            user_id, feconf.ROLE_ID_BLOG_POST_EDITOR)
+        self.assertEqual(
+            user_services.get_user_roles_from_id(user_id), [
+                feconf.ROLE_ID_FULL_USER, feconf.ROLE_ID_BLOG_POST_EDITOR])
+
+        expected_error_msg = 'The user already has this role.'
+        with self.assertRaisesRegex(Exception, expected_error_msg):
+            user_services.add_user_role(
+                user_id, feconf.ROLE_ID_BLOG_POST_EDITOR)
+
     def test_add_full_user_role_to_learner_raises_exception(self) -> None:
         auth_id = 'test_id'
         user_email = 'test@email.com'